11

I have a plotly plot composed of subplots -

fig = make_subplots(
        rows=3, cols=1)

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=2, col=1)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=3, col=1)

I was to add a title for every the subplots but only after the figure is created and the traces are added. I.e not when I create the figure -

fig = make_subplots(
            rows=3, cols=1, subplot_titles=['a', 'b', 'c']

Can I do it via fig.update_layout or something similar?

Tom Ron
  • 5,906
  • 3
  • 22
  • 38

4 Answers4

8

When make_subplots is used it's creating (correctly placed) annotations. So this can be mostly duplicated using annotation methods. In general, for the first:

fig.add_annotation(xref="x domain",yref="y domain",x=0.5, y=1.2, showarrow=False,
                   text="a", row=1, col=1)

The downside is you may need to adjust x and y to your conditions/tastes.

Full example, using bold text:

import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots(rows=3, cols=1)
    
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.add_annotation(xref="x domain",yref="y domain",x=0.5, y=1.2, showarrow=False,
                   text="<b>Hey</b>", row=1, col=1)

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=2, col=1)
fig.add_annotation(xref="x domain",yref="y domain",x=0.5, y=1.2, showarrow=False,
                   text="<b>Bee</b>", row=2, col=1)

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=3, col=1)
fig.add_annotation(xref="x domain",yref="y domain",x=0.5, y=1.2, showarrow=False,
                   text="<b>See</b>", row=3, col=1)

fig.show()

enter image description here

jayveesea
  • 2,886
  • 13
  • 25
0

You can iterate through the xaxes as follows. This sets the x axis title on each subgraph:

for i in range(3):
    fig['layout'][f'xaxis{i+1}'].update(title=f'Title {i+1}')

Related question setting showgrid on each axis: Plotly set showgrid = False for ALL subplots

David Parks
  • 30,789
  • 47
  • 185
  • 328
0

Specify anything for a title during make_subplot, as long as it is not an empty string. This will take care of calculating the right x and y for the annotation so that you don't have to figure it out yourself like in @jayveesea answer.

You only have to change the text of the title later on when it suits you.

The title of subplot is actually annotation "text". Just in case you were not seeing word title anywhere. :-)

import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots(rows=3, cols=1, subplot_titles=[" ", " ", " "])

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.layout.annotations[0].update(text="My Title 1")

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=2, col=1)
fig.layout.annotations[1].update(text="My Title 2")

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=3, col=1)      
fig.layout.annotations[2].update(text="My Title 3")

fig.show()

Example with several subplots

IvanD
  • 7,971
  • 4
  • 35
  • 33
-1

Try this,

fig.update_layout(margin=dict(l=20, r=20, t=20, b=20), paper_bgcolor="LightSteelBlue", xaxis_title='X Label Name', yaxis_title="Y Label Name")
Maran Sowthri
  • 829
  • 6
  • 14