{ "metadata": { "name": "", "signature": "sha256:a1c3bfa66f3d68d14043afc1c6ef963c3621cd3f212350f8f63d63230761ca82" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Warning!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This example notebook is not up-to-date with the latest version of Plotly's Python API (version 1.0.\\*). \n", "\n", "Refer to Plotly's Python [User Guide](http://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s00_homepage/s00_homepage.ipynb) \n", "and more specifically [section 7.1](http://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s7_streaming/s7_streaming_p1-first-stream.ipynb) for an updated version of this notebook.\n", "\n", "
" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Graphing Time-Series data with Plotly's Real-time Streaming API" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotly Streaming enables your plotly graphs to update in real-time, without refreshing your browser.\n", "\n", "Learn more about real-time streaming graphs with plotly here:\n", "\n", "[https://github.com/plotly/streaming-demos](https://github.com/plotly/streaming-demos)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import plotly\n", "import datetime\n", "import time\n", "import numpy as np\n", "import json" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Fill in the config.json file in this directory with your plotly username, \n", "# plotly API key, and your generated plotly streaming tokens\n", "# Sign up to plotly here: https://plot.ly/ssu\n", "# View your API key and streaming tokens here: https://plot.ly/settings\n", "\n", "with open('./config.json') as config_file:\n", " plotly_user_config = json.load(config_file)\n", "\n", "username = plotly_user_config['plotly_username'] \n", "api_key = plotly_user_config['plotly_api_key']\n", "stream_token = plotly_user_config['plotly_streaming_tokens'][3]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Initialize your plotly object\n", "p = plotly.plotly(username, api_key)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Initialize your plotly real-time streaming graph with a REST API call\n", "# Embed the stream token in one of the traces of a plotly-data object - one token per trace\n", "# Also embed 'maxpoints', the number of points that you want plotted at a time\n", "\n", "# The `iplot` command will embed our plotly graph as an iframe in this notebook\n", "# Each plotly graph has a unique url that you can share and anyone can view \n", "# your streaming graph in real-time\n", "\n", "# The unique URL for this graph is https://plot.ly/~streaming-demos/12\n", "\n", "p.iplot([{'x': [], 'y': [], 'type': 'scatter', 'mode': 'lines+markers',\n", " 'stream': {'token': stream_token, 'maxpoints': 80}\n", " }],\n", " filename='Time-Series', fileopt='overwrite')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Now stream! Write to a plotly stream object\n", "# Our data will be in the the form:\n", "# {'x': x_data, 'y':y_data}\n", "# Each point that we yield will get shipped through plotly's servers\n", "# to the graph your web-browser, updating it in real-time\n", "s = plotly.stream(stream_token)\n", "i=0\n", "k = 5\n", "while True:\n", " i+=1\n", " # log current time and a random number\n", " x_data_point = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')\n", " y_data_point = (np.cos(k*i/50.)*np.cos(i/50.)+np.random.randn(1))[0]\n", " \n", " s.write({'x': x_data_point, 'y': y_data_point})\n", " time.sleep(80./1000.)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# When you're done, close your stream!\n", "s.close()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Give it a try yourself!\n", "\n", "##### More Streaming IPython Notebooks:\n", "\n", "[http://nbviewer.ipython.org/github/plotly/Streaming-Demos/tree/master/IPython%20examples/](http://nbviewer.ipython.org/github/plotly/Streaming-Demos/tree/master/IPython%20examples/)\n", "\n", "##### More about Plotly Streaming \n", "\n", "[https://github.com/plotly/Streaming-Demos](https://github.com/plotly/Streaming-Demos)\n", "\n", "##### Get in touch!\n", "\n", "[http://twitter.com/plotlygraphs](@plotlygraphs)\n", "\n", "[https://facebook.com/plotly](https://facebook.com/plotly)\n", "\n", "" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# CSS styling within IPython notebook\n", "from IPython.core.display import HTML\n", "import urllib2\n", "def css_styling():\n", " url = 'https://raw.githubusercontent.com/plotly/python-user-guide/master/custom.css'\n", " styles = urllib2.urlopen(url).read()\n", " return HTML(styles)\n", "\n", "css_styling()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }