2025, Nov 20 07:00

Set xaxis.title.text and yaxis.title.text in Plotly graph_objects to add readable axis labels

Learn how to add custom axis titles in Plotly graph_objects by setting xaxis.title.text and yaxis.title.text in go.Layout. Clear labels for go.Figure plots.

Setting readable axis titles in Plotly is easy with plotly express, but when you switch to graph_objects and build a go.Figure by hand, the path is less obvious. If you’re plotting something custom, like an exponential decay, and want explicit control over labels, here’s how to do it cleanly inside go.Layout.

Problem

You have a go.Figure and want to add custom titles to the x and y axes. The figure renders fine, but the axes lack descriptive labels.

import numpy as np
import plotly.graph_objects as gobj
import plotly.io as pio
pio.templates.default = "none"

xs = np.linspace(0, 10, 100)

curve = gobj.Scatter(
    x=xs,
    y=100 * pow(1/2, xs),
    mode='lines',
    line_width=2,
    line_color='red',
)

layout_base = gobj.Layout(title='My Graph')

plot_obj = gobj.Figure(data=[curve], layout=layout_base)

# need custom axis titles here

plot_obj.show()

What’s happening and why

In graph_objects, axis titles are part of the layout tree. The x-axis and y-axis each have their own configuration blocks inside go.Layout. The text that appears as an axis label sits under xaxis.title.text and yaxis.title.text. If you don’t set those, axes render without custom titles.

Solution

Provide xaxis and yaxis configurations when creating the layout, and place your label strings into the nested title.text fields. This approach keeps everything within go.Layout and is explicit about where axis metadata lives.

import numpy as np
import plotly.graph_objects as gobj
import plotly.io as pio
pio.templates.default = "none"

xs = np.linspace(0, 10, 100)

curve = gobj.Scatter(
    x=xs,
    y=100 * pow(1/2, xs),
    mode='lines',
    line_width=2,
    line_color='red',
)

custom_layout = gobj.Layout(
    title='My Graph',
    xaxis={
        "title": {
            "text": "My custom x axis title"
        }
    },
    yaxis={
        "title": {
            "text": "My custom y axis title"
        }
    }
)

plot_obj = gobj.Figure(data=[curve], layout=custom_layout)

plot_obj.show()

If you prefer constructor syntax with dict(), the following is equivalent and works the same way:

import numpy as np
import plotly.graph_objects as gobj
import plotly.io as pio
pio.templates.default = "none"

xs = np.linspace(0, 10, 100)

curve = gobj.Scatter(
    x=xs,
    y=100 * pow(1/2, xs),
    mode='lines',
    line_width=2,
    line_color='red',
)

custom_layout = gobj.Layout(
    title='My Graph',
    xaxis=dict(
        title=dict(text="My custom x axis title")
    ),
    yaxis=dict(
        title=dict(text="My custom y axis title")
    )
)

plot_obj = gobj.Figure(data=[curve], layout=custom_layout)

plot_obj.show()

Why this matters

Axis titles are not decoration; they document intent. When you construct figures with graph_objects—especially for mathematical curves like exponential decay—labels prevent misinterpretation, make comparisons reliable, and keep plots self-explanatory in notebooks, reports, or dashboards.

Takeaways

For go.Figure, define axis titles directly in go.Layout by setting xaxis.title.text and yaxis.title.text. Keeping titles in the layout makes your configuration explicit and portable across figures, while preserving full control over how your axes read and render.