{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Check which version is installed on your machine and please upgrade if needed. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'1.9.10'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's load the dependencies/packages that we need in order to get a simple stream going."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import plotly.plotly as py \n",
"import plotly.tools as tls \n",
"import plotly.graph_objs as go\n",
"import numpy as np "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Overview\n",
"\n",
"In this example we're going to be streaming to two different types of traces on the same plotting surface via subplots. \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Getting Set Up"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's get at least two streaming tokens for this task."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0xhh453c6m\n",
"4lm5a0gsr8\n"
]
}
],
"source": [
"stream_tokens = tls.get_credentials_file()['stream_ids']\n",
"token_1 = stream_tokens[-1] # I'm getting my stream tokens from the end to ensure I'm not reusing tokens\n",
"token_2 = stream_tokens[-2] \n",
"print token_1\n",
"print token_2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's create some `stream id objects` for each token."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"stream_id1 = dict(token=token_1, maxpoints=60)\n",
"stream_id2 = dict(token=token_2, maxpoints=60)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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.\n",
"\n",
"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.\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"trace1 = go.Bar(\n",
" x=['one', 'two', 'three'],\n",
" y=[4, 3, 2],\n",
" xaxis='x2',\n",
" yaxis='y2',\n",
" marker=dict(color=\"maroon\"),\n",
" name='Random Numbers',\n",
" stream=stream_id2,\n",
" showlegend=False\n",
")\n",
"trace2 = go.Pie(\n",
" labels=['one','two','three'],\n",
" values=[20,50,100],\n",
" domain=dict(x=[0, 0.45]),\n",
" text=['one', 'two', 'three'],\n",
" stream=stream_id1,\n",
" sort=False,\n",
" \n",
")\n",
"data = [trace1, trace2]\n",
"layout = go.Layout(\n",
" xaxis2=dict(\n",
" domain=[0.5, 0.95],\n",
" anchor='y2'\n",
" ),\n",
" yaxis2=dict(\n",
" domain=[0, 1],\n",
" anchor='x2'\n",
" )\n",
")\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='simple-inset-stream')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's set up some `stream link objects` and start streaming some data to our plot"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"s_1 = py.Stream(stream_id=token_1)\n",
"s_2 = py.Stream(stream_id=token_2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Start Streaming"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"s_1.open()\n",
"s_2.open()\n",
"\n",
"import time\n",
"import datetime\n",
"import numpy as np\n",
"\n",
"while True:\n",
" nums = np.random.random_integers(0,10, size=(3))\n",
" s_1.write(dict(labels=['one', 'two', 'three'], values=nums, type='pie'))\n",
" s_2.write(dict(x=['one', 'two', 'three'], y=nums, type='bar', marker=dict(color=[\"blue\", \"orange\", \"green\"])))\n",
" time.sleep(0.8)\n",
"s_1.close()\n",
"s_2.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can see this stream live below:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tls.embed('streaming-demos','122')"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already up-to-date: publisher in /Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages\r\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/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.\n",
" \"You should import from nbconvert instead.\", ShimWarning)\n",
"/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.\n",
" warnings.warn('Did you \"Save\" this notebook before running this command? '\n",
"/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?\n",
" warnings.warn('Your URL has more than 2 parts... are you sure?')\n"
]
}
],
"source": [
"from IPython.display import display, HTML\n",
"\n",
"display(HTML(''))\n",
"display(HTML(''))\n",
"\n",
"! pip install publisher --upgrade\n",
"import publisher\n",
"publisher.publish(\n",
" 'subplot-streaming', 'python/subplot-streaming//', 'Streaming in Plotly',\n",
" 'Streaming in Plotly with Python', name=\"Streaming with Subplots\",\n",
" title = 'Streaming in Subplots with Plotly',\n",
" thumbnail='', language='python',\n",
" layout='user-guide', has_thumbnail='false',\n",
" ipynb= '~notebook_demo/83') "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}