6

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.

3 Answers 3

24

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 ;)

Sign up to request clarification or add additional context in comments.

3 Comments

In the first block of code you specify xref = 'x1' and yref = 'y2 while in the last one the returned dictionary has a value of xref: 'x2' and 'yref': 'y2'. Is this a typo?
Give this maam a cookie!
Im 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...
8

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

Comments

2

I think you can also set annotations by referencing the row and col of a specific subplot:

    fig.add_annotation(
        xref="x domain",
        yref="y domain",
        # The arrow head will be 25% along the x axis, starting from the left
        x=0.8,
        # The arrow head will be 40% along the y axis, starting from the bottom
        y=0.9,
        text="An annotation referencing the axes",
        # arrowhead=2,

        row=1,

        col=1
    )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.