This example notebook is not up-to-date with the latest version of Plotly's Python API (version 1.0.*).
Refer to Plotly's Python User Guide and more specifically section 7.0 and section 7.1 for an updated version of this notebook.
Plotly Streaming enables your plotly graphs to update in real-time, without refreshing your browser.
Learn more about real-time streaming graphs with plotly here:
import plotly
import json
import time
from math import cos, sin
print(plotly.__version__)
0.5.11
# Fill in the config.json file in this directory with your plotly username,
# plotly API key, and your generated plotly streaming tokens
# Sign up to plotly here: https://plot.ly/ssu
# View your API key and streaming tokens here: https://plot.ly/settings
with open('./config.json') as config_file:
plotly_user_config = json.load(config_file)
username = plotly_user_config['plotly_username']
api_key = plotly_user_config['plotly_api_key']
stream_token = plotly_user_config['plotly_streaming_tokens'][5]
stream_server = 'http://stream.plot.ly'
# Initialize your plotly object
p = plotly.plotly(username, api_key)
# Initialize your plotly real-time streaming graph with a REST API call
# Embed the stream token in one of the traces of a plotly-data object - one token per trace
# Also embed 'maxpoints', the number of points that you want plotted at a time
# The `iplot` command will embed our plotly graph as an iframe in this notebook
# Each plotly graph has a unique url that you can share and anyone can view
# your streaming graph in real-time
# The unique URL for this graph is https://plot.ly/~streaming-demos/1
p.iplot([{'x': [], 'y': [], 'type': 'scatter',
'stream': {'token': stream_token, 'maxpoints': 100}
}], layout={'xaxis': {'range':[-1,1]}, 'yaxis': {'range': [-1,1]}},
filename='Stream Example', fileopt='overwrite')
# Now stream! Initialize a plotly stream object with your token
s = plotly.stream(stream_token)
# Write a point and watch the plot update!
s.write({'x': 0.5, 'y': 0.5})
# Send a bunch of points
i=0
while True:
s.write({'x': cos(5*i/50.)*sin(i/50.), 'y': cos(5*i/50.)*cos(i/50.)})
i+=1
time.sleep(0.05)
# When your done, close your stream
s.close()
http://nbviewer.ipython.org/github/plotly/Streaming-Demos/tree/master/IPython%20examples/
https://github.com/plotly/Streaming-Demos
# CSS styling within IPython notebook
from IPython.core.display import HTML
import urllib2
def css_styling():
url = 'https://raw.githubusercontent.com/plotly/python-user-guide/master/custom.css'
styles = urllib2.urlopen(url).read()
return HTML(styles)
css_styling()