Python Updating a figure made using Plotly

  • Thread starter Thread starter Swamp Thing
  • Start date Start date
  • Tags Tags
    Figure
AI Thread 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,028
Reaction score
763
TL;DR Summary
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
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
11
Views
2K
Replies
3
Views
2K
Replies
10
Views
3K
Replies
6
Views
4K
Replies
17
Views
7K
Replies
2
Views
10K
Replies
2
Views
2K
Back
Top