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 ?
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()