Check which version is installed on your machine and please upgrade if needed.
import plotly
plotly.__version__
'1.9.10'
Now let's load the dependencies/packages that we need in order to get a simple stream going.
import plotly.plotly as py
import plotly.tools as tls
import plotly.graph_objs as go
import numpy as np
In this example we're going to be streaming to two different types of traces on the same plotting surface via subplots.
Let's get at least two streaming tokens for this task.
stream_tokens = tls.get_credentials_file()['stream_ids']
token_1 = stream_tokens[-1] # I'm getting my stream tokens from the end to ensure I'm not reusing tokens
token_2 = stream_tokens[-2]
print token_1
print token_2
0xhh453c6m 4lm5a0gsr8
Now let's create some stream id objects
for each token.
stream_id1 = dict(token=token_1, maxpoints=60)
stream_id2 = dict(token=token_2, maxpoints=60)
The set up of this plot will contain one pie chart on the left and a bar chart on the right that will display the same information. This is possible because they are both charts that display "counts" for categroical variables.
We will have three categories, which are brilliantly named 'one', 'two', and 'three'. Later we'll randomly generate count data and stream it to our plot.
import plotly.plotly as py
import plotly.graph_objs as go
trace1 = go.Bar(
x=['one', 'two', 'three'],
y=[4, 3, 2],
xaxis='x2',
yaxis='y2',
marker=dict(color="maroon"),
name='Random Numbers',
stream=stream_id2,
showlegend=False
)
trace2 = go.Pie(
labels=['one','two','three'],
values=[20,50,100],
domain=dict(x=[0, 0.45]),
text=['one', 'two', 'three'],
stream=stream_id1,
sort=False,
)
data = [trace1, trace2]
layout = go.Layout(
xaxis2=dict(
domain=[0.5, 0.95],
anchor='y2'
),
yaxis2=dict(
domain=[0, 1],
anchor='x2'
)
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='simple-inset-stream')
Now let's set up some stream link objects
and start streaming some data to our plot
s_1 = py.Stream(stream_id=token_1)
s_2 = py.Stream(stream_id=token_2)
s_1.open()
s_2.open()
import time
import datetime
import numpy as np
while True:
nums = np.random.random_integers(0,10, size=(3))
s_1.write(dict(labels=['one', 'two', 'three'], values=nums, type='pie'))
s_2.write(dict(x=['one', 'two', 'three'], y=nums, type='bar', marker=dict(color=["blue", "orange", "green"])))
time.sleep(0.8)
s_1.close()
s_2.close()
You can see this stream live below:
tls.embed('streaming-demos','122')
from IPython.display import display, HTML
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
! pip install publisher --upgrade
import publisher
publisher.publish(
'subplot-streaming', 'python/subplot-streaming//', 'Streaming in Plotly',
'Streaming in Plotly with Python', name="Streaming with Subplots",
title = 'Streaming in Subplots with Plotly',
thumbnail='', language='python',
layout='user-guide', has_thumbnail='false',
ipynb= '~notebook_demo/83')
Requirement already up-to-date: publisher in /Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages
/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated. You should import from nbconvert instead. "You should import from nbconvert instead.", ShimWarning) /Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning: Did you "Save" this notebook before running this command? Remember to save, always save. warnings.warn('Did you "Save" this notebook before running this command? ' /Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/publisher/publisher.py:58: UserWarning: Your URL has more than 2 parts... are you sure? warnings.warn('Your URL has more than 2 parts... are you sure?')