{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import plotly.plotly as py\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countryyearpopcontinentlifeExpgdpPercap
0 Afghanistan 1952 8425333 Asia 28.801 779.445314
1 Afghanistan 1957 9240934 Asia 30.332 820.853030
2 Afghanistan 1962 10267083 Asia 31.997 853.100710
3 Afghanistan 1967 11537966 Asia 34.020 836.197138
4 Afghanistan 1972 13079460 Asia 36.088 739.981106
\n", "

5 rows × 6 columns

\n", "
" ], "text/plain": [ " country year pop continent lifeExp gdpPercap\n", "0 Afghanistan 1952 8425333 Asia 28.801 779.445314\n", "1 Afghanistan 1957 9240934 Asia 30.332 820.853030\n", "2 Afghanistan 1962 10267083 Asia 31.997 853.100710\n", "3 Afghanistan 1967 11537966 Asia 34.020 836.197138\n", "4 Afghanistan 1972 13079460 Asia 36.088 739.981106\n", "\n", "[5 rows x 6 columns]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The datasets' url. Thanks Jennifer Bryan!\n", "url_csv = 'http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/examples/gapminder/data/gapminderDataFiveYear.txt'\n", "\n", "df = pd.read_csv(url_csv, sep='\\t')\n", "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's plot population as a function of the year for a few selected countries" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "countries = ['China', 'India', 'United States', 'Bangladesh', 'South Africa']\n", "fill_colors = ['#66c2a5', '#fc8d62', '#8da0cb', '#e78ac3', '#a6d854']\n", "gf = df.groupby('country')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data = []\n", "\n", "for country, fill_color in zip(countries[::-1], fill_colors):\n", " group = gf.get_group(country)\n", " years = group['year'].tolist()\n", " length = len(years)\n", " country_coords = [country] * length\n", " pop = group['pop'].tolist()\n", " zeros = [0] * length\n", " \n", " data.append(dict(\n", " type='scatter3d',\n", " mode='lines',\n", " x=years + years[::-1] + [years[0]], # year loop: in incr. order then in decr. order then years[0]\n", " y=country_coords * 2 + [country_coords[0]],\n", " z=pop + zeros + [pop[0]],\n", " name='',\n", " surfaceaxis=1, # add a surface axis ('1' refers to axes[1] i.e. the y-axis)\n", " surfacecolor=fill_color,\n", " line=dict(\n", " color='black',\n", " width=4\n", " ),\n", " ))\n", "\n", "layout = dict(\n", " title='Population from 1957 to 2007 [Gapminder]',\n", " showlegend=False,\n", " scene=dict(\n", " xaxis=dict(title=''),\n", " yaxis=dict(title=''),\n", " zaxis=dict(title=''),\n", " camera=dict(\n", " eye=dict(x=-1.7, y=-1.7, z=0.5)\n", " )\n", " )\n", ")\n", "\n", "fig = dict(data=data, layout=layout)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "py.iplot(fig, validate=False, filename='filled-3d-lines')" ] }, { "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.6" } }, "nbformat": 4, "nbformat_minor": 0 }