{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "___\n", "\n", " \n", "___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Choropleth Maps" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Offline Plotly Usage" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get imports and set everything up to be working offline." ] }, { "cell_type": "code", "execution_count": 251, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go \n", "from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now set up everything so that the figures show up in the notebook:" ] }, { "cell_type": "code", "execution_count": 252, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "init_notebook_mode(connected=True) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "More info on other options for Offline Plotly usage can be found [here](https://plot.ly/python/offline/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Choropleth US Maps\n", "\n", "Plotly's mapping can be a bit hard to get used to at first, remember to reference the cheat sheet in the data visualization folder, or [find it online here](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf)." ] }, { "cell_type": "code", "execution_count": 253, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we need to begin to build our data dictionary. Easiest way to do this is to use the **dict()** function of the general form:\n", "\n", "* type = 'choropleth',\n", "* locations = list of states\n", "* locationmode = 'USA-states'\n", "* colorscale= \n", "\n", "Either a predefined string:\n", "\n", " 'pairs' | 'Greys' | 'Greens' | 'Bluered' | 'Hot' | 'Picnic' | 'Portland' | 'Jet' | 'RdBu' | 'Blackbody' | 'Earth' | 'Electric' | 'YIOrRd' | 'YIGnBu'\n", "\n", "or create a [custom colorscale](https://plot.ly/python/heatmap-and-contour-colorscales/)\n", "\n", "* text= list or array of text to display per point\n", "* z= array of values on z axis (color of state)\n", "* colorbar = {'title':'Colorbar Title'})\n", "\n", "Here is a simple example:" ] }, { "cell_type": "code", "execution_count": 254, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data = dict(type = 'choropleth',\n", " locations = ['AZ','CA','NY'],\n", " locationmode = 'USA-states',\n", " colorscale= 'Portland',\n", " text= ['text1','text2','text3'],\n", " z=[1.0,2.0,3.0],\n", " colorbar = {'title':'Colorbar Title'})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we create the layout nested dictionary:" ] }, { "cell_type": "code", "execution_count": 255, "metadata": { "collapsed": true }, "outputs": [], "source": [ "layout = dict(geo = {'scope':'usa'})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we use: \n", "\n", " go.Figure(data = [data],layout = layout)\n", " \n", "to set up the object that finally gets passed into iplot()" ] }, { "cell_type": "code", "execution_count": 256, "metadata": { "collapsed": false }, "outputs": [], "source": [ "choromap = go.Figure(data = [data],layout = layout)" ] }, { "cell_type": "code", "execution_count": 257, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "iplot(choromap)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Real Data US Map Choropleth\n", "\n", "Now let's show an example with some real data as well as some other options we can add to the dictionaries in data and layout." ] }, { "cell_type": "code", "execution_count": 258, "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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
codestatecategorytotal exportsbeefporkpoultrydairyfruits freshfruits proctotal fruitsveggies freshveggies proctotal veggiescornwheatcottontext
0ALAlabamastate1390.6334.410.6481.04.068.017.125.115.58.914.3334.970.0317.61Alabama<br>Beef 34.4 Dairy 4.06<br>Fruits 25.1...
1AKAlaskastate13.310.20.10.00.190.00.00.000.61.01.560.00.00.00Alaska<br>Beef 0.2 Dairy 0.19<br>Fruits 0.0 Ve...
2AZArizonastate1463.1771.317.90.0105.4819.341.060.27147.5239.4386.917.348.7423.95Arizona<br>Beef 71.3 Dairy 105.48<br>Fruits 60...
3ARArkansasstate3586.0253.229.4562.93.532.24.76.884.47.111.4569.5114.5665.44Arkansas<br>Beef 53.2 Dairy 3.53<br>Fruits 6.8...
4CACaliforniastate16472.88228.711.1225.4929.952791.85944.68736.40803.21303.52106.7934.6249.31064.95California<br>Beef 228.7 Dairy 929.95<br>Frui...
\n", "
" ], "text/plain": [ " code state category total exports beef pork poultry dairy \\\n", "0 AL Alabama state 1390.63 34.4 10.6 481.0 4.06 \n", "1 AK Alaska state 13.31 0.2 0.1 0.0 0.19 \n", "2 AZ Arizona state 1463.17 71.3 17.9 0.0 105.48 \n", "3 AR Arkansas state 3586.02 53.2 29.4 562.9 3.53 \n", "4 CA California state 16472.88 228.7 11.1 225.4 929.95 \n", "\n", " fruits fresh fruits proc total fruits veggies fresh veggies proc \\\n", "0 8.0 17.1 25.11 5.5 8.9 \n", "1 0.0 0.0 0.00 0.6 1.0 \n", "2 19.3 41.0 60.27 147.5 239.4 \n", "3 2.2 4.7 6.88 4.4 7.1 \n", "4 2791.8 5944.6 8736.40 803.2 1303.5 \n", "\n", " total veggies corn wheat cotton \\\n", "0 14.33 34.9 70.0 317.61 \n", "1 1.56 0.0 0.0 0.00 \n", "2 386.91 7.3 48.7 423.95 \n", "3 11.45 69.5 114.5 665.44 \n", "4 2106.79 34.6 249.3 1064.95 \n", "\n", " text \n", "0 Alabama
Beef 34.4 Dairy 4.06
Fruits 25.1... \n", "1 Alaska
Beef 0.2 Dairy 0.19
Fruits 0.0 Ve... \n", "2 Arizona
Beef 71.3 Dairy 105.48
Fruits 60... \n", "3 Arkansas
Beef 53.2 Dairy 3.53
Fruits 6.8... \n", "4 California
Beef 228.7 Dairy 929.95
Frui... " ] }, "execution_count": 258, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('2011_US_AGRI_Exports')\n", "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now out data dictionary with some extra marker and colorbar arguments:" ] }, { "cell_type": "code", "execution_count": 259, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data = dict(type='choropleth',\n", " colorscale = 'YIOrRd',\n", " locations = df['code'],\n", " z = df['total exports'],\n", " locationmode = 'USA-states',\n", " text = df['text'],\n", " marker = dict(line = dict(color = 'rgb(255,255,255)',width = 2)),\n", " colorbar = {'title':\"Millions USD\"}\n", " ) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And our layout dictionary with some more arguments:" ] }, { "cell_type": "code", "execution_count": 260, "metadata": { "collapsed": true }, "outputs": [], "source": [ "layout = dict(title = '2011 US Agriculture Exports by State',\n", " geo = dict(scope='usa',\n", " showlakes = True,\n", " lakecolor = 'rgb(85,173,240)')\n", " )" ] }, { "cell_type": "code", "execution_count": 261, "metadata": { "collapsed": true }, "outputs": [], "source": [ "choromap = go.Figure(data = [data],layout = layout)" ] }, { "cell_type": "code", "execution_count": 262, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "iplot(choromap)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# World Choropleth Map\n", "\n", "Now let's see an example with a World Map:" ] }, { "cell_type": "code", "execution_count": 263, "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", "
COUNTRYGDP (BILLIONS)CODE
0Afghanistan21.71AFG
1Albania13.40ALB
2Algeria227.80DZA
3American Samoa0.75ASM
4Andorra4.80AND
\n", "
" ], "text/plain": [ " COUNTRY GDP (BILLIONS) CODE\n", "0 Afghanistan 21.71 AFG\n", "1 Albania 13.40 ALB\n", "2 Algeria 227.80 DZA\n", "3 American Samoa 0.75 ASM\n", "4 Andorra 4.80 AND" ] }, "execution_count": 263, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('2014_World_GDP')\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 264, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data = dict(\n", " type = 'choropleth',\n", " locations = df['CODE'],\n", " z = df['GDP (BILLIONS)'],\n", " text = df['COUNTRY'],\n", " colorbar = {'title' : 'GDP Billions US'},\n", " ) " ] }, { "cell_type": "code", "execution_count": 265, "metadata": { "collapsed": false }, "outputs": [], "source": [ "layout = dict(\n", " title = '2014 Global GDP',\n", " geo = dict(\n", " showframe = False,\n", " projection = {'type':'Mercator'}\n", " )\n", ")" ] }, { "cell_type": "code", "execution_count": 266, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "choromap = go.Figure(data = [data],layout = layout)\n", "iplot(choromap)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Great Job!" ] } ], "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.1" } }, "nbformat": 4, "nbformat_minor": 0 }