{ "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", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n" ] } ], "source": [ "%load_ext autoreload\n", "%autoreload 2\n", "\n", "import pandas as pd\n", "import datetime\n", "import os\n", "import numpy as np\n", "from StringIO import StringIO\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", "import matplotlib.pyplot as plt\n", "\n", "# Data retrieved from http://www.quandl.com/api/v1/datasets/ODA/DEU_PCPIPCH.csv?column=1\n", "data = \"\"\"Date,Value\\n2019-12-31,1.7\\n2018-12-31,1.7\\n2017-12-31,1.7\\n2016-12-31,1.5\\n2015-12-31,1.247\\n2014-12-31,0.896\\n2013-12-31,1.601\\n2012-12-31,2.13\\n2011-12-31,2.498\\n2010-12-31,1.158\\n2009-12-31,0.226\\n2008-12-31,2.738\\n2007-12-31,2.285\\n2006-12-31,1.784\\n2005-12-31,1.92\\n2004-12-31,1.799\\n2003-12-31,1.022\\n2002-12-31,1.346\\n2001-12-31,1.904\\n2000-12-31,1.418\\n1999-12-31,0.626\\n1998-12-31,0.593\\n1997-12-31,1.542\\n1996-12-31,1.19\\n1995-12-31,1.733\\n1994-12-31,2.717\\n1993-12-31,4.476\\n1992-12-31,5.046\\n1991-12-31,3.474\\n1990-12-31,2.687\\n1989-12-31,2.778\\n1988-12-31,1.274\\n1987-12-31,0.242\\n1986-12-31,-0.125\\n1985-12-31,2.084\\n1984-12-31,2.396\\n1983-12-31,3.284\\n1982-12-31,5.256\\n1981-12-31,6.324\\n1980-12-31,5.447\\n\"\"\"\n", "df = pd.read_csv(StringIO(data), index_col=0, parse_dates=True)\n", "df = df.sort_index()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic examples" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display_charts(df, title=\"Germany inflation rate\")" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display_charts(df, chart_type=\"stock\", title=\"Germany inflation rate\")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display_charts(df, kind=\"bar\", title=\"Germany inflation rate\")" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display_charts(df, kind=\"barh\", title=\"Germany inflation rate\")" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display_charts(df, title=\"Germany inflation rate\", legend=None, kind=\"bar\", figsize = (400, 200))" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display_charts(df, title=\"Germany inflation rate\", kind=\"bar\", render_to=\"chart5\", zoom=\"xy\")" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Data retrieved from https://www.quandl.com/api/v1/datasets/CVR/ANGEL_SECTORS.csv\n", "data = \"\"\"Year,Software,Healthcare,Hardware,Biotech,Telecom,Manufacturing,Financial Products and Services,IT Services,Industrial/Energy,Retail,Media\\n2013-12-31,23.0,14.0,,11.0,,,7.0,,,7.0,16.0\\n2012-12-31,23.0,14.0,,11.0,,,,,7.0,12.0,7.0\\n2011-12-31,23.0,19.0,,13.0,,,,7.0,13.0,,5.0\\n2010-12-31,16.0,30.0,,15.0,,,,5.0,8.0,5.0,\\n2009-12-31,19.0,17.0,,8.0,,,5.0,,17.0,9.0,\\n2008-12-31,13.0,16.0,,11.0,,,,,8.0,12.0,7.0\\n2007-12-31,27.0,19.0,,12.0,,,,,8.0,6.0,5.0\\n2006-12-31,18.0,21.0,,18.0,,,6.0,,6.0,8.0,\\n2005-12-31,18.0,20.0,8.0,12.0,,,,6.0,6.0,,6.0\\n2004-12-31,22.0,16.0,10.0,10.0,6.0,,8.0,8.0,,7.0,\\n2003-12-31,26.0,13.0,12.0,11.0,5.0,12.0,,,,,\\n2002-12-31,40.0,14.0,5.0,5.0,5.0,,,,,,\\n\"\"\"\n", "df3 = pd.read_csv(StringIO(data), index_col=0, parse_dates=True)\n", "df3 = df3.fillna(0) / 100\n", "df4 = pd.DataFrame(df3.mean(), columns=['ratio'])\n", "df4['total'] = 1" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display_charts(df4, kind='pie', y=['ratio'], title='Angel Deals By Sector', tooltip={'pointFormat': '{series.name}: {point.percentage:.1f}%'})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Highcharts specific" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df4 = pd.DataFrame(df3.sum(), columns=['sum'])\n", "#df4.to_dict('series').items()[0][1].tolist()\n", "display_charts(df4, polar=True, kind='bar', ylim=(0, 2.3), title='Angel Deals By Sector')" ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 0 }