Python Updating a figure made using Plotly

  • Thread starter Thread starter Swamp Thing
  • Start date Start date
  • Tags Tags
    Figure
Click For Summary
To update a Plotly graph in Python, specifically in Google Colab, the common method of using `f1.update_traces(y=[4,3,2,1])` does not reflect changes unless `f1.show()` is called, which generates a new plot instead of updating the existing one. Modifying the data directly with `f1.data[0].y = [4, 3, 2, 1]` appears to work, as confirmed by the output showing the updated 'y' values. However, the plot itself does not visually update to reflect these changes. It is noted that Google Colab may not support live updates of plots in the same manner as traditional Jupyter notebooks, leading to challenges in dynamically updating visualizations.
Swamp Thing
Insights Author
Messages
1,045
Reaction score
775
TL;DR
I'm trying to update a plotly graph with new data:
f1.update_traces(y=[4,3,2,1])
Nothing happens.
After making a graph using Plotly in Python, I'd like to change the data. From the documentation (which is not very clear) it appears this is the code we need:

f1.update_traces( y=[4,3,2,1] )
(where f1 is the figure created previously).

But nothing happens when I run this.

In order to see the new data, I find that I need to do f1.show() after the above step. The problem with this is that it shows a new plot rather than changing the old one, so each time I change the data it goes into a new plot.

So I guess my big-picture question is, using Python in Google Colab, how can I change attributes (including data) of an existing plot in situ?
 
Technology news on Phys.org
You should be able to just modify the data in place:
Python:
f1.data[0].y = [4, 3, 2, 1]
 
pbuk said:
You should be able to just modify the data in place:
Python:
f1.data[0].y = [4, 3, 2, 1]

I've tried that too.
Python:
f1.data[0].y=[7,6,5,9]
print(f1.data[0])

The output from the print statement shows that the 'y' array has been written into correctly:
Code:
Scatter({
    'uid': '289cc751-4c63-4126-8235-82c69798e9d6', 'x': [2, 3, 4, 5], 'y': [7, 6, 5, 9]
})

... but the plot doesn't reflect the change.

Are you using Python in a notebook? I'm using Google Colab, which perhaps doesn't support live updating of plots...
 
Swamp Thing said:
I'm using Google Colab, which perhaps doesn't support live updating of plots...
No it doesn't, at least not in the same way as a 'normal' Jupyter notebook does.
 
  • Informative
Likes Swamp Thing
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
908
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 17 ·
Replies
17
Views
7K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
10K
  • · Replies 2 ·
Replies
2
Views
2K