How can I set title for subplots

code like this

from plotly import data
import plotly.graph_objects as go

df = data.stocks()

layout = dict(
    hoversubplots="axis",
    title="Stock Price Changes",
    hovermode="x unified",
    grid=dict(rows=2, columns=1),
)

data = [
    go.Scatter(x=df["date"], y=df["AAPL"], xaxis="x", yaxis="y1", name="Apple"),
    go.Scatter(x=df["date"], y=df["AMZN"], xaxis="x", yaxis="y2", name="Amazon"),
]

fig = go.Figure(data=data, layout=layout)

fig.show()```

I want to set title for two subplots, how should I do ?

My usual approach is this:

from plotly.subplots import make_subplots
fig = make_subplots(rows=2, cols=1, subplot_titles = ['Plot 1', 'Plot 2'])
fig.update_layout(...)
fig.add_traces(...)

I donโ€™t know how to update subplot titles after the figure has been created - hoping someone might have a way of doing that

thanks. I tried to use make_subplots to create fig object. But hovermode=โ€œx unifiedโ€ did not work โ€ฆ

Yes, getting Plotly to do exactly what you want can be a pain. This code makes โ€œx unifiedโ€ work. But it doesnโ€™t put the x axis labels where you probably want them, and Iโ€™m afraid I donโ€™t know how to make it all come together just right:

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

fig2 = make_subplots(rows=2, cols=1, subplot_titles = ['Apple', 'Amazon'])
fig2.update_layout(hoversubplots="axis", title="Stock Price Changes", hovermode="x unified")
fig2.add_traces([
    go.Scatter(x=df["date"], y=df["AAPL"], xaxis="x", yaxis="y1", name="Apple"),
    go.Scatter(x=df["date"], y=df["AMZN"], xaxis="x", yaxis="y2", name="Amazon"),
])
fig2.show()
1 Like

Hi @Kesqqq ,

Based on your very first sample code,
here the modification if you want to add subtitles using plotly graph objects :

  1. Add annotations to set subplot titles.
  2. Adjust vertical gap in grid using ygap attribute
  3. set proper vertical position of every subplot titles by setting y value in annotations
from plotly import data
import plotly.graph_objects as go

df = data.stocks()

layout = dict(
    hoversubplots="axis",
    title="Stock Price Changes",
    hovermode="x unified",
    grid=dict(rows=2, columns=1, ygap=0.2), # 2. adding vertical gap (ygap) between plots in grids
    annotations = [{'font': {'size': 16}, # 1. adding annotations to set subtitles
                                'showarrow': False,
                                'text': 'Apple',
                                'x': 0.5,
                                'xanchor': 'center',
                                'xref': 'paper',
                                'y': 1.0,
                                'yanchor': 'bottom',
                                'yref': 'paper'},
                               {'font': {'size': 16},
                                'showarrow': False,
                                'text': 'Amazon',
                                'x': 0.5,
                                'xanchor': 'center',
                                'xref': 'paper',
                                'y': 0.450, # 3. set proper vertical position based on your display output 
                                'yanchor': 'bottom',
                                'yref': 'paper'}]
)



data = [
    go.Scatter(x=df["date"], y=df["AAPL"], xaxis="x", yaxis="y1", name="Apple"),
    go.Scatter(x=df["date"], y=df["AMZN"], xaxis="x", yaxis="y2", name="Amazon"),
]

fig = go.Figure(data=data, layout=layout)

fig.show()

Hope this solution give more options to generate subplot titles.