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.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 datetime
import time
import numpy as np
import json
# 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'][3]
# 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/12
p.iplot([{'x': [], 'y': [], 'type': 'scatter', 'mode': 'lines+markers',
'stream': {'token': stream_token, 'maxpoints': 80}
}],
filename='Time-Series', fileopt='overwrite')
# Now stream! Write to a plotly stream object
# Our data will be in the the form:
# {'x': x_data, 'y':y_data}
# Each point that we yield will get shipped through plotly's servers
# to the graph your web-browser, updating it in real-time
s = plotly.stream(stream_token)
i=0
k = 5
while True:
i+=1
# log current time and a random number
x_data_point = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
y_data_point = (np.cos(k*i/50.)*np.cos(i/50.)+np.random.randn(1))[0]
s.write({'x': x_data_point, 'y': y_data_point})
time.sleep(80./1000.)
# When you're 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()