# Plot real-time data

**URL:** <https://community.plotly.com/t/plot-real-time-data/31467>\
**Category:** 📊 Plotly Python\
**Created:** [November 18, 2019, 12:49am UTC](https://community.plotly.com/t/plot-real-time-data/31467 "2019-11-18T00:49:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ursus](https://avatars.discourse-cdn.com/v4/letter/u/e99b99/32.png) [@ursus](https://community.plotly.com/u/ursus)\
**Post date:** [November 18, 2019, 12:49am UTC](https://community.plotly.com/t/plot-real-time-data/31467/1 "2019-11-18T00:49:52Z")

</div>

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:

```python
import time
import plotly.graph_objects as go

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

```

Initialize the plot:

```python
fig = go.FigureWidget()
fig.add_scatter()
fig

```

Dynamic updating:

**Method I**

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

```

**Method II**

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

```

Both produce the following dynamic plot:

 ![Peek%202019-11-18%2001-41](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/0/06b1716c12946b6d20ae99ca3a96e7bb171a952d.gif)

**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?

---

<div class="post-metadata">

**Author:** ![Emmanuelle](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/emmanuelle/32/7853_2.png) [@Emmanuelle](https://community.plotly.com/u/Emmanuelle)\
**Post date:** [November 18, 2019, 3:55am UTC](https://community.plotly.com/t/plot-real-time-data/31467/2 "2019-11-18T03:55:13Z")

</div>

You can also take a look at Dash applications ([https://dash.plot.ly](https://dash.plot.ly)). For example, this app [https://dash-gallery.plotly.host/dash-wind-streaming/](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](https://github.com/plotly/dash-sample-apps/blob/master/apps/dash-wind-streaming/app.py)

---

<div class="post-metadata">

**Author:** ![ursus](https://avatars.discourse-cdn.com/v4/letter/u/e99b99/32.png) [@ursus](https://community.plotly.com/u/ursus)\
**Post date:** [November 22, 2019, 8:47pm UTC](https://community.plotly.com/t/plot-real-time-data/31467/3 "2019-11-22T20:47:44Z")

</div>

That is a very nice example, Emmanuelle.  
I also found this one: [https://dash.plot.ly/live-updates](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 🙂

---

<div class="post-metadata">

**Author:** ![Emmanuelle](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/emmanuelle/32/7853_2.png) [@Emmanuelle](https://community.plotly.com/u/Emmanuelle)\
**Post date:** [November 23, 2019, 3:23am UTC](https://community.plotly.com/t/plot-real-time-data/31467/4 "2019-11-23T03:23:01Z")

</div>

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