Hi @JackTang ,
You can set specific axis reference on annotation using yref
or xref
.
Based on the previous example I just set the xref="paper"
for all subplot.
By setting specific yref
based on the yaxis at every subplot which are y,y2,y3,y3...
.
You can put annotation based on points on every subplots.
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
from plotly.subplots import make_subplots
df = px.data.stocks()
fig = make_subplots(rows=3, cols=1)
fig.add_trace(
go.Scatter(x=df['date'], y=df["GOOG"]),
row=1, col=1
)
fig.add_trace(
go.Scatter(x=df['date'], y=df["GOOG"]),
row=2, col=1
)
fig.add_trace(
go.Scatter(x=df['date'], y=df["GOOG"]),
row=3, col=1
)
last_point = round(df["GOOG"].values[-1],4)
median = np.median(df["GOOG"].values)
annotations = []
# Last point
annotations.append(dict(
xref='paper',
yref='y',
x=1.05,
y=last_point,
xanchor='right',
yanchor='middle',
text='{}'.format(last_point),
showarrow=False,
font=dict(size=16,color='blue')
))
# Left Y axis title
annotations.append(dict(
xref='paper',
yref='y',
x=-0.04,
y=median,
xanchor='left',
yanchor='middle',
textangle=-90,
text='{}'.format("Y Axis 1"),
showarrow=False,
font=dict(size=16,color='blue')
))
# Last point 2
annotations.append(dict(
xref='paper',
yref='y2',
x=1.05,
y=last_point,
xanchor='right',
yanchor='middle',
text='{}'.format(last_point),
showarrow=False,
font=dict(size=16,color='red')
))
# Left Y axis title 2
annotations.append(dict(
xref='paper',
yref='y2',
x=-0.04,
y=median,
xanchor='left',
yanchor='middle',
textangle=-90,
text='{}'.format("Y Axis 2"),
showarrow=False,
font=dict(size=16,color='red')
))
# Last point 3
annotations.append(dict(
xref='paper',
yref='y3',
x=1.05,
y=last_point,
xanchor='right',
yanchor='middle',
text='{}'.format(last_point),
showarrow=False,
font=dict(size=16,color='green')
))
# Left Y axis title 3
annotations.append(dict(
xref='paper',
yref='y3',
x=-0.04,
y=median,
xanchor='left',
yanchor='middle',
textangle=-90,
text='{}'.format("Y Axis 3"),
showarrow=False,
font=dict(size=16,color='green')
))
fig.update_layout(showlegend=False, annotations=annotations)
fig.update_yaxes(title_text="",side="right", row=1, col=1)
fig.update_yaxes(title_text="", side="right",row=2, col=1)
fig.update_yaxes(title_text="", side="right",row=3, col=1)
fig.show()