Extending data to the same plot


#1

Hello,

I’m new to plotly and I’m trying to update a plot (a single line that has a new point every time) when I receive my data.

The ting is, I want to start plotting before receiving all my data points and update ‘real-time’ when I get them.

The only thing close I have managed to do is:

import plotly.plotly as py
from plotly.graph_objs import *

i=0
while i<10:
    new_data = dict(x=[3+i,4+i], y=[3+i,4+i] )
    data = [ new_data ]
    i = i+1
    plot_url = py.plot(data, filename='extend plot', fileopt='extend')

However, this opens a new tab for each time py.plot is called and I can’t find a way to live update the plot in the same tab.

Thank you for the help!


#2

Hi @bertaserracanta,

If you’re working in the Jupyter Notebook, the easiest way to do this would be to use a FigureWidget

First display a figure with an empty scatter trace

import plotly.graph_objs as go
import time
fig = go.FigureWidget(data=[go.Scatter(x=[], y=[])])
fig

Then update the data

i=0
scatter = fig.data[0]

while i<10:
    new_x = [3+i, 4+i]
    new_y = [3+i, i-4]
    with fig.batch_update():
        scatter.x += tuple(new_x)
        scatter.y += tuple(new_y)
    time.sleep(1)
    i = i+1

If you’re not working in the Jupyter Notebook, and you want a standalone web app. You could also accomplish the same this using Dash https://dash.plot.ly/.

Hope that helps,

-Jon