✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Subplot titles disappearing when adding annotations

I am creating two subplots, both scatter. Then I add two annotations.

The problem is that subplot titles aren’t shown, unless I remove the code for adding the annotations.

I use:

fig = py.tools.make_subplots(rows = 1, cols= 2, subplot_titles = (“Original data”, “Log-transformed data”))

fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 2)

fig[“layout”].update(annotations = [
go.Annotation(
x = 0.5,
y = -0.2,
showarrow=False,
text=“Text”,
xref=“paper”,
yref=“paper”
),
go.Annotation(
x = -0.05,
y = 0.15,
showarrow=False,
text=“Text”,
xref=“paper”,
yref=“paper”,
textangle=-90
)
])

Why is this happening? How can I fix it?

Thanks.

@WindChimes,
fig['layout']['annotations']is a list. Hence you should extend that list, in order to preserve the annotations defined when the fig was created (particularly subplot_titles).

 fig['layout']['annotations'].extend(  [
       go.Annotation(
      x = 0.5,
      y = -0.2,
      showarrow=False,
      text=“Text”,
      xref=“paper”,
      yref=“paper”
       ),
     go.Annotation(
     x = -0.05,
     y = 0.15,
    showarrow=False,
     text=“Text”,
    xref=“paper”,
    yref=“paper”,
    textangle=-90
  )])
1 Like

How do we classify while extending that the 1st annotation should be on the 1st subplot, and the second annotation to be on the second subplot?

When i do straight extend, both the annotations comes on the top subplot and the second subplot is blank?

@mixy Before choosing the annotation position you should inspect the domain for x and y-plot coordinates in each subplot, as it is explained in this notebook https://plot.ly/~empet/14826.

@empet: does this still work? I am trying:

fig[“layout”][“annotations”].extend(
dict(
x=-0.075,
y=0.45,
showarrow=False,
text=“testing”,
textangle=-90,
xref=“paper”,
yref=“paper”,
font=dict(color=“black”, size=18),
)
)

And getting the error:

—> 65 fig[“layout”][“annotations”].extend([
66 dict(
67 x=-0.075,

AttributeError: ‘tuple’ object has no attribute ‘extend’

@agplot, If you are running Plotly 3.+, the answer is negative. First you should define separately the list
of annotations, i.e.:

annotations=[]
annotations.append({...})

or

annotations.extend([{...}, {...}])

and finally, update fig.layout:

fig.layout.annotations=annotations

@empet, Thanks for your response. It is still not 100% clear how to get both the subplot titles and the new annotation.

Should I start by creating the subplot titles, then convert fig.layout.annotations to a list (instead of a tuple), and finally extend with my new annotation?

@agplot, You should proceed as follows:
Don’t pass subplot_titles to tls.make_subplotst().
Then define annotations as I mentioned in the previous answer, where you include both annotations for titles, and other annotations you intended to add before as updates. When the lists of annotations is complete, then you update the fig.layout with:

fig.layout.annotations=annotations

Hi @empet,

I found a decent workaround. I use tls.make_subplots() with subplot_titles. Then,

annotations = [a.to_plotly_json() for a in fig["layout"]["annotations"]]
annotations.append({...})
fig["layout"]["annotations"] = annotations

Thanks for working through this with me.

1 Like

For future reference, another option you can use here is the += operator.

fig["layout"]["annotations"] += ({...},) # Note the trailing comma

-Jon

Hi there, I just encountered the same problem as mentioned in this thread but
fig[“layout”][“annotations”] += [{…}]
did not work for me but
fig[“layout”][“annotations”] += ({…})

did

Greetings, Alex

Thanks, Alex! You’re right and I’ve updated by post above :slightly_smiling_face:
-Jon