Created
December 12, 2016 17:09
-
-
Save sglyon/d0015451dd2a79e88f077a1b1e15337b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window.Plotly) {{require(['plotly'],function(plotly) {window.Plotly=plotly;});}}</script>" | |
| ], | |
| "text/plain": [ | |
| "<IPython.core.display.HTML object>" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "from io import StringIO\n", | |
| "import pandas as pd\n", | |
| "from plotly.offline import iplot\n", | |
| "import plotly.graph_objs as go\n", | |
| "import plotly\n", | |
| "plotly.offline.init_notebook_mode(connected=True)\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 13, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "data = StringIO(\"\"\"Year,KOR,JPN,IND,USA,CHN\n", | |
| "2000,44.7,29.99,0.53,43.08,1.78\n", | |
| "2001,56.6,38.53,0.66,49.08,2.64\n", | |
| "2002,59.4,46.59,1.54,58.79,4.6\n", | |
| "2003,65.5,48.44,1.69,61.7,6.2\n", | |
| "2004,70.2,62.39,1.98,64.76,7.3\n", | |
| "2005,72.8,66.92,2.39,67.97,8.52\n", | |
| "2006,74.1,68.69,2.81,68.93,10.52\n", | |
| "2007,75.5,74.3,3.95,75.0,16.0\n", | |
| "2008,76.5,75.4,4.38,74.0,22.6\n", | |
| "2009,77.2,78.0,5.12,71.0,28.9\n", | |
| "2010,77.8,78.21,7.5,71.69,34.3\n", | |
| "2011,78.0,79.05,10.07,69.73,38.3\n", | |
| "2012,78.4,79.5,12.58,74.7,42.3\n", | |
| "2013,82.1,88.22,15.1,71.4,45.8\n", | |
| "2014,83.6,89.11,21.0,73.0,47.9\n", | |
| "2015,85.1,93.33,26.0,74.55,50.3\n", | |
| "\"\"\")\n", | |
| "df = pd.read_csv(data, index_col=0)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 14, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def trace_for_year(year):\n", | |
| " data = df.loc[year, :]\n", | |
| " out = dict(type=\"choropleth\",\n", | |
| " locations=data.index,\n", | |
| " locationmode=\"ISO-3\",\n", | |
| " z=data,\n", | |
| " colorscale= \"Reds\",\n", | |
| " )\n", | |
| " return out" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 16, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div id=\"46e976b4-0681-40fa-b7ac-4b95cd8d3c67\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";\n", | |
| " Plotly.plot(\n", | |
| " '46e976b4-0681-40fa-b7ac-4b95cd8d3c67',\n", | |
| " [{\"type\": \"choropleth\", \"locations\": [\"KOR\", \"JPN\", \"IND\", \"USA\", \"CHN\"], \"colorscale\": \"Reds\", \"locationmode\": \"ISO-3\", \"z\": [44.7, 29.99, 0.53, 43.08, 1.78]}],\n", | |
| " {\"title\": \"Percent of Population that Accessed the Internet in 2000\", \"sliders\": [{\"steps\": [{\"label\": \"2000\", \"method\": \"animate\", \"args\": [[\"2000\"], {\"frame\": {\"duration\": 0}}]}, {\"label\": \"2001\", \"method\": \"animate\", \"args\": [[\"2001\"], {\"frame\": {\"duration\": 0}}]}, {\"label\": \"2002\", \"method\": \"animate\", \"args\": [[\"2002\"], {\"frame\": {\"duration\": 0}}]}, {\"label\": \"2003\", \"method\": \"animate\", \"args\": [[\"2003\"], {\"frame\": {\"duration\": 0}}]}, {\"label\": \"2004\", \"method\": \"animate\", \"args\": [[\"2004\"], {\"frame\": {\"duration\": 0}}]}, {\"label\": \"2005\", \"method\": \"animate\", \"args\": [[\"2005\"], {\"frame\": {\"duration\": 0}}]}, {\"label\": \"2006\", \"method\": \"animate\", \"args\": [[\"2006\"], {\"frame\": {\"duration\": 0}}]}, {\"label\": \"2007\", \"method\": \"animate\", \"args\": [[\"2007\"], {\"frame\": {\"duration\": 0}}]}, {\"label\": \"2008\", \"method\": \"animate\", \"args\": [[\"2008\"], {\"frame\": {\"duration\": 0}}]}, {\"label\": \"2009\", \"method\": \"animate\", \"args\": [[\"2009\"], {\"frame\": {\"duration\": 0}}]}, {\"label\": \"2010\", \"method\": \"animate\", \"args\": [[\"2010\"], {\"frame\": {\"duration\": 0}}]}, {\"label\": \"2011\", \"method\": \"animate\", \"args\": [[\"2011\"], {\"frame\": {\"duration\": 0}}]}, {\"label\": \"2012\", \"method\": \"animate\", \"args\": [[\"2012\"], {\"frame\": {\"duration\": 0}}]}, {\"label\": \"2013\", \"method\": \"animate\", \"args\": [[\"2013\"], {\"frame\": {\"duration\": 0}}]}, {\"label\": \"2014\", \"method\": \"animate\", \"args\": [[\"2014\"], {\"frame\": {\"duration\": 0}}]}, {\"label\": \"2015\", \"method\": \"animate\", \"args\": [[\"2015\"], {\"frame\": {\"duration\": 0}}]}], \"currentvalue\": {\"prefix\": \"Year: \"}}], \"geo\": {\"scope\": \"asia\", \"resolution\": 50, \"showcountries\": true, \"showframe\": true, \"landcolor\": \"rgb(229, 229, 229)\", \"showland\": true}},\n", | |
| " {\"showLink\": true, \"linkText\": \"Export to plot.ly\"}\n", | |
| " ).then(function () {return Plotly.addFrames('46e976b4-0681-40fa-b7ac-4b95cd8d3c67',[{\"traces\": [0], \"layout\": {\"title\": \"Percent of Population that Accessed the Internet in 2000\"}, \"data\": [{\"z\": [44.7, 29.99, 0.53, 43.08, 1.78]}], \"name\": \"2000\"}, {\"traces\": [0], \"layout\": {\"title\": \"Percent of Population that Accessed the Internet in 2001\"}, \"data\": [{\"z\": [56.6, 38.53, 0.66, 49.08, 2.64]}], \"name\": \"2001\"}, {\"traces\": [0], \"layout\": {\"title\": \"Percent of Population that Accessed the Internet in 2002\"}, \"data\": [{\"z\": [59.4, 46.59, 1.54, 58.79, 4.6]}], \"name\": \"2002\"}, {\"traces\": [0], \"layout\": {\"title\": \"Percent of Population that Accessed the Internet in 2003\"}, \"data\": [{\"z\": [65.5, 48.44, 1.69, 61.7, 6.2]}], \"name\": \"2003\"}, {\"traces\": [0], \"layout\": {\"title\": \"Percent of Population that Accessed the Internet in 2004\"}, \"data\": [{\"z\": [70.2, 62.39, 1.98, 64.76, 7.3]}], \"name\": \"2004\"}, {\"traces\": [0], \"layout\": {\"title\": \"Percent of Population that Accessed the Internet in 2005\"}, \"data\": [{\"z\": [72.8, 66.92, 2.39, 67.97, 8.52]}], \"name\": \"2005\"}, {\"traces\": [0], \"layout\": {\"title\": \"Percent of Population that Accessed the Internet in 2006\"}, \"data\": [{\"z\": [74.1, 68.69, 2.81, 68.93, 10.52]}], \"name\": \"2006\"}, {\"traces\": [0], \"layout\": {\"title\": \"Percent of Population that Accessed the Internet in 2007\"}, \"data\": [{\"z\": [75.5, 74.3, 3.95, 75.0, 16.0]}], \"name\": \"2007\"}, {\"traces\": [0], \"layout\": {\"title\": \"Percent of Population that Accessed the Internet in 2008\"}, \"data\": [{\"z\": [76.5, 75.4, 4.38, 74.0, 22.6]}], \"name\": \"2008\"}, {\"traces\": [0], \"layout\": {\"title\": \"Percent of Population that Accessed the Internet in 2009\"}, \"data\": [{\"z\": [77.2, 78.0, 5.12, 71.0, 28.9]}], \"name\": \"2009\"}, {\"traces\": [0], \"layout\": {\"title\": \"Percent of Population that Accessed the Internet in 2010\"}, \"data\": [{\"z\": [77.8, 78.21, 7.5, 71.69, 34.3]}], \"name\": \"2010\"}, {\"traces\": [0], \"layout\": {\"title\": \"Percent of Population that Accessed the Internet in 2011\"}, \"data\": [{\"z\": [78.0, 79.05, 10.07, 69.73, 38.3]}], \"name\": \"2011\"}, {\"traces\": [0], \"layout\": {\"title\": \"Percent of Population that Accessed the Internet in 2012\"}, \"data\": [{\"z\": [78.4, 79.5, 12.58, 74.7, 42.3]}], \"name\": \"2012\"}, {\"traces\": [0], \"layout\": {\"title\": \"Percent of Population that Accessed the Internet in 2013\"}, \"data\": [{\"z\": [82.1, 88.22, 15.1, 71.4, 45.8]}], \"name\": \"2013\"}, {\"traces\": [0], \"layout\": {\"title\": \"Percent of Population that Accessed the Internet in 2014\"}, \"data\": [{\"z\": [83.6, 89.11, 21.0, 73.0, 47.9]}], \"name\": \"2014\"}, {\"traces\": [0], \"layout\": {\"title\": \"Percent of Population that Accessed the Internet in 2015\"}, \"data\": [{\"z\": [85.1, 93.33, 26.0, 74.55, 50.3]}], \"name\": \"2015\"}]);}).then(function(){Plotly.animate('46e976b4-0681-40fa-b7ac-4b95cd8d3c67');})\n", | |
| " });</script>" | |
| ], | |
| "text/plain": [ | |
| "<IPython.core.display.HTML object>" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "frames = [\n", | |
| " dict(\n", | |
| " data=[{\"z\": trace_for_year(year)[\"z\"].tolist()}],\n", | |
| " traces=[0],\n", | |
| " name=str(year),\n", | |
| " layout=dict(title=\"Percent of Population that Accessed the Internet in {}\".format(year))\n", | |
| " )\n", | |
| " for year in df.index.tolist()\n", | |
| "]\n", | |
| "\n", | |
| "layout2 = go.Layout(\n", | |
| " title=\"Percent of Population that Accessed the Internet in 2000\",\n", | |
| " geo={\n", | |
| " \"scope\": \"asia\", \n", | |
| " \"resolution\": 50, \n", | |
| " \"landcolor\":\"rgb(229, 229, 229)\",\n", | |
| " \"showland\": True, \n", | |
| " \"showframe\":True,\n", | |
| " \"showcountries\": True\n", | |
| " },\n", | |
| " sliders=[\n", | |
| " dict(\n", | |
| " currentvalue={\"prefix\": \"Year: \"},\n", | |
| " steps=[\n", | |
| " dict(\n", | |
| " label=f[\"name\"],\n", | |
| " method=\"animate\",\n", | |
| " args=[[f[\"name\"]], {\"frame\": {\"duration\": 0}}]\n", | |
| " )\n", | |
| " for f in frames\n", | |
| " ]\n", | |
| " ),\n", | |
| " \n", | |
| " ]\n", | |
| ")\n", | |
| "\n", | |
| "fig = go.Figure(data=[trace_for_year(2000)], layout=layout2, frames=frames)\n", | |
| "iplot(fig)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.5.2" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment