farhawa
farhawa

Reputation: 10398

python - plotly: remove plotted data from graph

I am trying to plot online data coming from two different sources in this way:

In each iteration:

1] first source: add incoming data to the graph.

2] second source: delete previous data and keep only the incoming ones in the graph.

here is my code:

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

import numpy as np
from sklearn.cluster import MeanShift, estimate_bandwidth
from sklearn.datasets.samples_generator import make_blobs
import time

py.sign_in('wajdi', 'key')

trace1 = Scatter(
    x=[],
    y=[],
    mode='markers',
    marker=Marker(
        color=[],
        opacity=1,
        line=Line(width=0.0),
        size=[],symbol='circle'),
    stream=Stream(token='troi4k35qp'),
)

trace2 = Scatter(
    x=[],
    y=[],
    mode='markers',
    marker=Marker(color=[]),
    stream=Stream(token='8lsvl6a79e')
)
data = Data([trace1,trace2])

layout = Layout(title='Time Series')

fig = Figure(data=data, layout=layout)

unique_url = py.plot(fig, filename='s7_first-stream')

s = py.Stream('8lsvl6a79e')
z = py.Stream('troi4k35qp')

s.open()
z.open()
print "waiting for brower..."
time.sleep(6)

centers = [[15, 15],[5, 5]]

for i in range(1000):
    X, _ = make_blobs(n_samples=2, centers=centers, cluster_std=1.0,center_box=(-1, 1))
    print i
    s.write(dict(x=X[0,0], y=X[0,1],marker=Marker(color='blue')))
    z.write(dict(x=X[1,0], y=X[1,1],marker=Marker(color='green',size=15))) 
    time.sleep(0.9)

    # I WANT TO REMOVE DATA WRITED IN Z STREAM HERE

s.close()
z.close()
print "finish"

Upvotes: 2

Views: 5914

Answers (1)

etpinard
etpinard

Reputation: 5011

You can remove (or clear if you prefer) data in a stream by sending empty arrays. In your case, simply add

z.write(x=[], y=[])

For a full example, refer to this ipython notebook:

Upvotes: 2

Related Questions