{ "metadata": { "name": "", "signature": "sha256:b3f2eb511087611efbff784ec6d12ed1e4bb36dacb1db92faf0b14915537d7e8" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Pandas Highcharts Example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Use [Highcharts](http://highcharts.com) to plot [pandas](http://pandas.pydata.org) DataFrame\n", "* Code on Github at [pandas-highcharts](https://github.com/gtnx/pandas-highcharts)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import pandas as pd\n", "import datetime\n", "from urllib2 import urlopen\n", "from IPython.display import display, display_pretty, Javascript, HTML\n", "from pandas_highcharts.core import serialize\n", "from pandas_highcharts.display import display_charts\n", "\n", "df = pd.read_csv(\n", " urlopen('http://www.quandl.com/api/v1/datasets/ODA/DEU_PCPIPCH.csv?column=1'),\n", " index_col=0, parse_dates=True\n", ")" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Use the display_charts function" ] }, { "cell_type": "code", "collapsed": false, "input": [ "display_charts(df, title=\"Germany inflation rate\")" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", " " ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "display_charts(df, chart_type=\"stock\", title=\"Germany inflation rate\")" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", " " ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "display_charts(df, kind=\"bar\", title=\"Germany inflation rate\")" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", " " ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "display_charts(df, kind=\"barh\", title=\"Germany inflation rate\")" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", " " ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "display_charts(df, title=\"Germany inflation rate\", legend=None, kind=\"bar\", figsize = (400, 200))" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", " " ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "display_charts(df, title=\"Germany inflation rate\", kind=\"bar\", render_to=\"chart5\", zoom=\"xy\")" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", " " ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "df = pd.DataFrame([\n", " {\"a\": 1, \"b\": 2, \"c\": 3, \"t\": datetime.datetime(2015, 1, 1, 0)},\n", " {\"a\": 2, \"b\": 4, \"c\": 6, \"t\": datetime.datetime(2015, 1, 1, 1)}\n", "])\n", "display_charts(df, x=\"t\", y=[\"a\", \"c\"], kind=\"bar\")" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", " " ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get Serialized Data an Plot with the IPython Display Feature" ] }, { "cell_type": "code", "collapsed": false, "input": [ "data = serialize(df, render_to=\"chart1\", kind=\"barh\", output_type=\"json\")" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "data.keys()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "['series', 'yAxis', 'chart', 'xAxis', 'legend']" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "data" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "{'chart': {'renderTo': 'chart1', 'type': 'bar'},\n", " 'legend': {'enabled': True},\n", " 'series': [{'data': [(0, 1), (1, 2)], 'name': 'a', 'yAxis': 0},\n", " {'data': [(0, 3), (1, 6)], 'name': 'c', 'yAxis': 0},\n", " {'data': [(0, 2), (1, 4)], 'name': 'b', 'yAxis': 0}],\n", " 'xAxis': {},\n", " 'yAxis': [{}]}" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "js_data = serialize(df, render_to=\"chart1\", kind=\"barh\")" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the display HTML IPython feature." ] }, { "cell_type": "code", "collapsed": false, "input": [ "display(HTML(\"\"\"\n", "
\n", "\n", "\"\"\" % locals()))" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "
\n", "\n" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 13 } ], "metadata": {} } ] }