Updating a figure made using Plotly

  • Context: Python 
  • Thread starter Thread starter Swamp Thing
  • Start date Start date
  • Tags Tags
    Figure
Click For Summary

Discussion Overview

The discussion revolves around updating a Plotly figure in Python, specifically within the Google Colab environment. Participants explore methods to change the data of an existing plot without generating a new plot each time.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests using the method f1.update_traces( y=[4,3,2,1] ) to update the data, but notes that it results in a new plot being displayed.
  • Another participant proposes modifying the data in place with f1.data[0].y = [4, 3, 2, 1], but reports that the plot does not reflect this change.
  • A later reply confirms that the output shows the 'y' array has been updated correctly, but questions whether Google Colab supports live updating of plots.
  • Participants discuss the limitations of Google Colab compared to a standard Jupyter notebook regarding plot updates.

Areas of Agreement / Disagreement

Participants generally agree that Google Colab does not support live updating of plots in the same manner as traditional Jupyter notebooks, but there is uncertainty about the effectiveness of the proposed methods for updating data.

Contextual Notes

Limitations include the potential differences in functionality between Google Colab and Jupyter notebooks, as well as the unclear behavior of Plotly when attempting to update existing figures.

Swamp Thing
Insights Author
Messages
1,047
Reaction score
786
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   Reactions: Swamp Thing

Similar threads

  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
975
  • · 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