# 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()
fig
``````

Dynamic updating:

Method I

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

Method II

``````for i in range(len(data)):
time.sleep(0.3)
with fig.batch_update():
fig.data.y = data[:i]
``````

Both produce the following dynamic plot:

Questions:

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 (https://dash.plot.ly). For example, this app https://dash-gallery.plotly.host/dash-wind-streaming/ queries a database for updates and then updates the figure. The source code is here https://github.com/plotly/dash-sample-apps/blob/master/apps/dash-wind-streaming/app.py

2 Likes

That is a very nice example, Emmanuelle.
I also found this one: https://dash.plot.ly/live-updates.

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

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 https://plot.ly/python-api-reference/generated/plotly.graph_objects.Figure.html#plotly.graph_objects.Figure.batch_update for more details.

1 Like
{"ft_lang_label":"__label__en","ft_lang_prob":0.8239854,"math_prob":0.45821548,"size":1521,"snap":"2023-14-2023-23","text_gpt3_token_len":376,"char_repetition_ratio":0.11667766,"word_repetition_ratio":0.009090909,"special_character_ratio":0.23865877,"punctuation_ratio":0.18429004,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9629978,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-25T14:06:03Z\",\"WARC-Record-ID\":\"<urn:uuid:86f3fe39-c297-43ab-abf3-f105e62a8f60>\",\"Content-Length\":\"32788\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:eed56355-796a-4bfb-a2b8-cc89e4b5c422>\",\"WARC-Concurrent-To\":\"<urn:uuid:e4e328cc-335b-4aad-95e3-330cfd79ca1e>\",\"WARC-IP-Address\":\"184.105.176.45\",\"WARC-Target-URI\":\"https://community.plotly.com/t/plot-real-time-data/31467\",\"WARC-Payload-Digest\":\"sha1:FZWBOGXU4M6W2W2PYZBEFXYFRRWBDZHB\",\"WARC-Block-Digest\":\"sha1:K6L3E7YFBJJT4OP34TX3UDIRGR73R6OZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296945333.53_warc_CC-MAIN-20230325130029-20230325160029-00379.warc.gz\"}"}