{ "metadata": { "name": "", "signature": "sha256:9a96a50f975c80838a44d491c288043ca4b18dbbb36cfd754c8ee319c7e30cb8" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Python datetime and Plotly" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "do's and don'ts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, import a few modules from the Plotly python package:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import plotly.plotly as py\n", "import plotly.tools as tls\n", "from plotly.graph_objs import *" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "The wrong way" ] }, { "cell_type": "code", "collapsed": false, "input": [ "trace1 = Scatter(\n", " x = ['07/11-07:59:40', '07/11-07:59:42', '07/11-07:59:50', '07/11-08:00:00'],\n", " y = ['10','20','5','15']\n", ")\n", " \n", "trace2 = Scatter(\n", " x = ['07/11-07:59:40', '07/11-07:59:43', '07/11-07:59:51', '07/11-08:00:00'],\n", " y = ['1','10','5','10']\n", ")\n", "\n", "data = Data([trace1, trace2])\n", "\n", "fig = Figure(data= data)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "py.iplot(fig, filename='netflix_datetimes1')" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, the x-coordinates are taken as strings. \n", "\n", "As with all string coordinates send to Plotly, each unique x element correspond to a coordinate point. The inferred coordinates are created in sequence, from the first element to last element and from the first trace to the last.\n", "\n", "In other words, Plotly cannot infer that `'07/11-07:59:43'` is in fact smaller than `'07/11-07:59:50'`." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "The right way" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotly understands Python datetimes (built with the `datetime` package of the Standard Library). More info on Python datetime can be found [here](https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior).\n", "\n", "So," ] }, { "cell_type": "code", "collapsed": false, "input": [ "from datetime import datetime\n", "\n", "def convert_to_datetime(times):\n", " return [datetime.strptime(time+' 2014', '%m/%d-%X %Y') for time in times] \n", "\n", "# Note that we must the year to each datetime in order to fully define them\n", "\n", "trace1 = Scatter(\n", " x = convert_to_datetime(['07/11-07:59:40', \n", " '07/11-07:59:42', \n", " '07/11-07:59:50', \n", " '07/11-08:00:00']),\n", " y = ['10','20','5','15']\n", ")\n", " \n", "trace2 = Scatter(\n", " x = convert_to_datetime(['07/11-07:59:40', \n", " '07/11-07:59:43', \n", " '07/11-07:59:51', \n", " '07/11-08:00:00']),\n", " y = ['1','10','5','10']\n", ")\n", "\n", "data = Data([trace1, trace2])\n", "\n", "fig = Figure(data= data)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "py.iplot(fig, filename='netflix_datetimes2')" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "The right way (2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotly also understands a specific string format.\n", "\n", "If you have the chance to modify the string format, this solution does not require the `datetime` package:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "trace1 = Scatter(\n", " x = ['2014-07-11 07:59:40', \n", " '2014-07-11 07:59:42', \n", " '2014-07-11 07:59:50', \n", " '2014-07/11 08:00:00'],\n", " y = ['10','20','5','15']\n", ")\n", " \n", "trace2 = Scatter(\n", " x = ['2014-07-11 07:59:40', \n", " '2014-07-11 07:59:43', \n", " '2014-07-11 07:59:51', \n", " '2014-07-11 08:00:00'],\n", " y = ['1','10','5','10']\n", ")\n", "\n", "data = Data([trace1, trace2])\n", "\n", "fig = Figure(data= data)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "py.iplot(fig, filename='netflix_datetimes3')" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "To learn more about Plotly's Python API" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Refer to\n", "\n", "* our online documentation page or\n", "* our User Guide." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "
\n", "\n", "

Got Questions or Feedback?

\n", "\n", "About Plotly\n", "\n", "* email: feedback@plot.ly \n", "* tweet: \n", "@plotlygraphs\n", "\n", "

Notebook styling ideas

\n", "\n", "Big thanks to\n", "\n", "* Cam Davidson-Pilon\n", "* Lorena A. Barba\n", "\n", "
" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import display, HTML\n", "import urllib2\n", "url = 'https://raw.githubusercontent.com/plotly/python-user-guide/master/custom.css'\n", "display(HTML(urllib2.urlopen(url).read()))" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }