graph_objs.Layout.update() method not updating annotations properly
See original GitHub issueWhen trying to update annotations within plotly.graph_objs.Layout using the .update() method, the annotations do not appear to update properly. This may be related to issue https://github.com/plotly/plotly.js/issues/1010
To clarify, it functions correctly if the len(annotations) is the same. However, if say 1 annotation was in the original layout, but the new layout has 2 annoations, only the first is updated and the second won’t appear. Perhaps an internal len(annotations) is being used and not updated?
An example is shown below (I’m using Jupyter):
import plotly
import plotly.graph_objs as go
print(plotly.__version__)
plotly.offline.init_notebook_mode() # run at the start of every ipython notebook
# Create random data with numpy
import numpy as np
N = 50
random_x = np.random.randn(N)
random_y = np.random.randn(N)
# Create a trace
trace = go.Scatter(
x = random_x,
y = random_y,
mode = 'markers'
)
data = [trace]
originalAnnotations=[
go.Annotation(x=.5, y=1, xref='paper',yref='paper', text='First Annotation',showarrow=False,)
]
differentAnnotations=[
go.Annotation(x=.5, y=1, xref='paper',yref='paper', text='New First Annotation',showarrow=False,),
go.Annotation(x=.5, y=.95, xref='paper',yref='paper', text='Second Annotation',showarrow=False,)
]
layout=go.Layout(
yaxis = dict(zeroline = False),
annotations=originalAnnotations,
)
# update the layout with different annotations now.
layout.update(annotations=differentAnnotations)
# where's the second annotation?? doesn't show up in the graph
plotly.offline.iplot({"data": data,"layout": layout})
# let's try it this way
layout['annotations']=differentAnnotations
plotly.offline.iplot({"data": data,"layout": layout})
#success, but layout.update() method broken?
2.0.9
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:7
Top Results From Across the Web
[Solved] Annotations not updating through callback
I have a donut chart that represents a % and updates itself every 10 secs. I put an anotattion in the middle that...
Read more >Shiny and Plotly update buttons are not updating with correct ...
Below is my code, I'm so close, the buttons will filter the data, but not correctly, so I feel like I'm missing something...
Read more >plotly package — 5.11.0 documentation - Plotly Help Center
If an annotation is added but annotation_position is not specified, this defaults to “top right”. ... Context manager to animate trace / layout...
Read more >Visualization with Plotly.Express: Comprehensive guide
A detailed guide on how to create many visualizations with Plotly Express with layout styling, interactivity, animations, and many chart ...
Read more >plotly multiple plots
A subplot () function is a wrapper function which allows the programmer to ... add_fun: Apply function to plot, without modifying data add_annotations:...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello, I’m on plotly 3.7.1 and ran into this very issue so I believe the update method is still broken, at least in the case of subplots. If I call
fig = tools.make_subplots(rows=n_rows,cols=n_cols, subplot_titles=[titles])
Then the annotations are restricted to the length of the list of subplot_titles and if I call update on the annotations then it only updates as many elements in my new annotations as were originally added to the figure.
Thanks for the reply @empet . I like your solution as it allows for an update method as one would expect. I’ll apply this in my own code.
I’m keeping the issue open as you’ve confirmed that Layout.update() seems to not be working as expected. It’s only a minor annoyance that can be worked around as you’ve shown. Nonetheless, it should probably get fixed at some point if users are going to be encouraged to use Plotly’s subclassed dictionaries.
Thanks again!