Update_traces after fig.show does not affect the plot

Hello,
I’m very new to plotly and I’m trying to refresh the data in shown plot.
In the docs I’ve found the update_traces function which looked promising to me. When I run the following example code the plot is showing in the browser with the first data. But afterwards the plot is not changed anymore.

import time
import plotly.graph_objects as go

# Fist Data
r1 = [1, 2, 3, 4, 5]
theta1 = [0, 45, 90, 135, 180]

print("Creating and showing Plot with first data")
fig = go.Figure()
fig.add_trace(go.Scatterpolar(r=r1, theta=theta1, mode='markers', marker=dict(size=20, color="MediumPurple")))
fig.show()

time.sleep(3)
print("Update to second data")
r2 = [2, 3, 4, 5, 6]
theta2 = [0, 30, 60, 90, 120]
fig.update_traces(r=r2, theta=theta2, marker=dict(size=10, color="LightSeaGreen"))

time.sleep(3)
print("Update to third data")
r3 = [4, 5, 6]
theta3 = [90, 135, 170]
fig.update_traces(r=r3, theta=theta3, marker=dict(size=20, color="MediumPurple"))

When I debug the script and take a look into the fig after I did the update_traces, I can see that the data for r and theta has changed. So the function does what it is supposed to do, unfortunately I can only see changes if i run fig.show() after each update_traces.

Is there a way to update the data in one plot without opening a new plot in the browser each time?

Big thanks in advance.

Does this do what you need? If not, an alternative might be to put the plot in a Dash app and use dcc.Interval to trigger a callback that updates the plot.

1 Like

Hey, thanks for the response.
I looked into the animations, but this is not what I need. I’m trying to plot live data that is getting updated every 100 ms. So instead of the animations the dcc interval/dash is looking like the piece I was missing.
I will try it and update the post.

OK, hope it works.

There have been other posts here recently (if I remember right) discussing possible issues / challenges with using dcc.Interval for rapid updates.

Websockets (outside my experience) may be another (better?) way of plotting live data, and this doc page describes this sort of application:

https://www.dash-extensions.com/components/websocket