Black Lives Matter. Please consider donating to Black Girls Code today.

Plot real-time data

What is the proper way to produce a Real-Time Plot, i.e. dynamically update the plot as new data arrives?

What I have so far is this:

import time
import plotly.graph_objects as go

data = [1,3,2,4,3,3,2,3]

Initialize the plot:

fig = go.FigureWidget()

Dynamic updating:

Method I

for i in range(len(data)):
    time.sleep(0.3)[0].y = data[:i] 

Method II

for i in range(len(data)):
    with fig.batch_update():[0].y = data[:i]

Both produce the following dynamic plot:


  1. What is the difference between these 2 methods?
  2. Which is the advisable method?
  3. This works in Jupyter environments. How do I implement something like this outside of Jupyter?
1 Like

You can also take a look at Dash applications ( For example, this app queries a database for updates and then updates the figure. The source code is here


That is a very nice example, Emmanuelle.
I also found this one:

But, I still wold like to know the answer to the first question if you or anyone else can help :slight_smile:

For your first question (what is the difference between using fig.batch_update and updating directly the trace data), here it does not make much of a difference because you only have one method call to update the FigureWidget. Using fig.batch_update is interesting when you have several update calls, because then they are batched together in a single patch sent only once to update the figure. See the docstring of fig.batch_update and its examples for more details.

1 Like