Welcome to Plotly's Python API User Guide.
Links to the other sections can be found on the User Guide's homepage
Section 7 is divided, into separate notebooks, as follows:
Quickstart (initialize Plotly figure object and send 1 data point through a stream):
>>> import plotly.plotly as py
>>> from plotly.graph_objs import *
>>> # auto sign-in with credentials or use py.sign_in()
>>> trace1 = Scatter(
x=[],
y=[],
stream=dict(token='my_stream_id')
)
>>> data = Data([trace1])
>>> py.plot(data)
>>> s = py.Stream('my_stream_id')
>>> s.open()
>>> s.write(dict(x=1, y=2))
>>> s.close()
Check which version is installed on your machine and please upgrade if needed.
# (*) Import plotly package
import plotly
# Check plolty version (if not latest, please upgrade)
plotly.__version__
'1.9.5'
Plotly's Streaming API enables your Plotly plots to update in real-time, without refreshing your browser. In other words, users can continuously send data to Plotly's servers and visualize this data in real-time.
For example, imagine that you have a thermometer (hooked to an Arduino for example) in your attic and you would like to monitor the temperature readings from your laptop. Plotly together with its streaming API makes this project easy to achieve.
With Ploly's Streaming API:
Everyone looking at a Plotly streaming plot sees the same data updating at the same time. Like all Plotly plots, Plotly streaming plots are immediately shareable by shortlink, embedded in website, or in an IPython notebook. Owners of the Plotly plot can edit the plot with the Plotly web GUI and all viewers will see these changes live.
And for the skeptical among us, it's fast: plots update up to 20 times per second.
In this section, we present examples of how to make Plotly streaming plots. Readers looking for info about the nuts and bolts of Plotly's streaming API should refer to this link.
So, we first import a few modules and sign in to Plotly using our credentials file:
# (*) To communicate with Plotly's server, sign in with credentials file
import plotly.plotly as py
# (*) Useful Python/Plotly tools
import plotly.tools as tls
# (*) Graph objects
from plotly.graph_objs import *
import numpy as np # (*) numpy for math functions and arrays
# Embed an existing Plotly streaming plot
tls.embed('streaming-demos','6')
# Note that the time point correspond to internal clock of the servers,
# that is UTC time.
Data is sent in real-time.
Plotly draws the data in real-time.
Plotly's interactibility happens in real-time.
Making Plotly streaming plots requires no modifications to the sign in process; however, users must generate stream tokens or ids. To do so, first sign-in to plot.ly. Once that is done, click on the Settings button in the upper-right corner of the page:
Under the Stream Tokens tab, click on the Generate Token button:
And there you go, you have generated a stream token. Please note that:
You must generate one stream token per trace in your Plotly streaming plot.
If you are looking to run this notebook with you own account, please generate 4 unique stream tokens and add them to your credentials file by entering:
>>> tls.set_credentials_file(stream_ids=[
"ab4kf5nfdn",
"kdf5bn4dbn",
"o72o2p08y5",
"81dygs4lcy"
])
where the stream_ids
keyword argument is filled in with your own stream ids.
Note that, in the above, tls.set_credentials()
overwrites the existing stream tokens (if any) but does not clear the other keys in your credentials file such as username
and api_key
.
Once your credentials file is updated with your stream tokens (or stream ids, a synonym), retrieve them as a list:
stream_ids = tls.get_credentials_file()['stream_ids']
We are now ready to start making Plotly streaming plots!
The content of this section has been divided into separate IPython notebooks as loading multiple streaming plots at once can cause performance slow downs on some internet connections.
Here are the links to the subsections' notebooks:
In addition, here is a notebook of another Plotly streaming plot:
Reach us here at: Plotly Community
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(
's7_streaming.ipynb', 'python/intro_streaming//', 'Getting Started with Plotly Streaming',
'Getting Started with Plotly Streaming',
title = 'Getting Started with Plotly Streaming',
thumbnail='', language='python',
layout='user-guide', has_thumbnail='false')
Requirement already up-to-date: publisher in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
/Library/Frameworks/Python.framework/Versions/2.7/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) /Library/Frameworks/Python.framework/Versions/2.7/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? ' /Library/Frameworks/Python.framework/Versions/2.7/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?')