How do I annotate each subplot separately in Plotly (Python)
Solution 1
1) You could assign annotation to specific subplot through setting xref
and yref
with subplot axis id, such as x1
and y1
represents x axis and y axis of subplot1, as seen from example below and more on link
fig['layout'].update(
annotations=[
dict(
x=2, y=2, # annotation point
xref='x1',
yref='y1',
text='dict Text',
showarrow=True,
arrowhead=7,
ax=10,
ay=70
),
dict(
...
# if have multiple annotations
)
])
2) After you assigned it, you could get access to annotations through
fig['layout']['annotations']
which will return a list of dictionary items:
[{'xref': 'x2', 'arrowhead': 7, 'yref': 'y2', 'text': 'dict Text', 'ay': 40, 'ax': 10, 'y': -1.9491807521563174, 'x': 0.77334098360655923, 'showarrow': True}, {'xref': 'x2', 'arrowhead': 7, 'yref': 'y2', 'text': 'dict Text', 'ay': -40, 'ax': 10, 'y': -0.0041268527747384542, 'x': 1.1132422279202281, 'showarrow': True}]
Hope this could help ;)
Solution 2
it also works with update(), if you adress the subplot as an element inside the annotations list.
from plotly.subplots import make_subplots
import plotly.graph_objects as go
# create figure with subplots
fig = make_subplots(rows=1, cols=2, subplot_titles = ['title1','title2'])
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[4, 5, 6]),
row=1, col=1
)
fig.add_trace(
go.Scatter(x=[20, 30, 40], y=[50, 60, 70]),
row=1, col=2
)
fig.update_layout(height=600, width=800, title_text="Subplots")
fig.show()
# to change subtitle, address subplot
fig['layout']['annotations'][0].update(text='your text here');
fig.show()
ᴊᴇsᴘᴇʀᴋ.ᴇᴛʜ
An engineer very passionate about writing code to solve problems. Strengths are Python and Unix. Passionate about Crypto as well.
Updated on June 08, 2022Comments
-
ᴊᴇsᴘᴇʀᴋ.ᴇᴛʜ over 1 year
I am trying to associate a separate annotation object with each subplot in Plotly (Python), how can this be done?
What I tried
I am setting up the plot like this:
from plotly import tools fig = tools.make_subplots(rows=2, cols=1) fig.append_trace(traces[0], 1, 1) fig.append_trace(traces[1], 2, 1)
where each trace is formed like this:
import plotly.graph_objs as go traces[0] = go.Scatter( x=[1,2,3,4], y=[4,4,2,1], mode='markers' )
I know I can access the xaxis of each subplot separately via:
fig['layout']['xaxis1'].update(title='hello1') fig['layout']['xaxis2'].update(title='hello2')
But how can I access the annotation of each subplot? I tried "annotations1" and "annotation1", with no luck. I also tried to access the layout of subplot 1 via "layout1" as in:
fig['layout1'][...].update(...)
This did not work either.
-
jsga almost 4 yearsIn the first block of code you specify
xref = 'x1' and yref = 'y2
while in the last one the returned dictionary has a value ofxref: 'x2'
and'yref': 'y2'
. Is this a typo? -
rubmz over 3 yearsGive this maam a cookie!
-
mike01010 over 2 yearsIm trying to display some information in the upper left of each subplot such as the the date and last value in each series on that subplot..using the above, it seems to show the text in lower left, but then the timeseries lines or bars dissappear...