Skip to content

Commit 13d80fb

Browse files
authored
Merge pull request #879 from plotly/choropleth-docs
Choropleth Docs
2 parents fdb2fa4 + 448dd64 commit 13d80fb

13 files changed

+1785
-473
lines changed

_posts/python/maps/choropleth-maps/2015-06-30-Choropleth_maps.html

Lines changed: 95 additions & 128 deletions
Large diffs are not rendered by default.

_posts/python/maps/choropleth-maps/Choropleth_maps.ipynb

100755100644
Lines changed: 70 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,18 @@
1414
"cell_type": "markdown",
1515
"metadata": {},
1616
"source": [
17-
"### United States Choropleth Map"
17+
"#### United States Choropleth Map"
1818
]
1919
},
2020
{
2121
"cell_type": "code",
2222
"execution_count": 1,
23-
"metadata": {
24-
"collapsed": false
25-
},
23+
"metadata": {},
2624
"outputs": [
2725
{
2826
"data": {
2927
"text/html": [
30-
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~chelsea_lyn/7898.embed\" height=\"525px\" width=\"100%\"></iframe>"
28+
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~AdamKulidjian/6694.embed\" height=\"525px\" width=\"100%\"></iframe>"
3129
],
3230
"text/plain": [
3331
"<plotly.tools.PlotlyDisplay object>"
@@ -89,20 +87,18 @@
8987
"cell_type": "markdown",
9088
"metadata": {},
9189
"source": [
92-
"### World Choropleth Map"
90+
"#### World Choropleth Map"
9391
]
9492
},
9593
{
9694
"cell_type": "code",
9795
"execution_count": 2,
98-
"metadata": {
99-
"collapsed": false
100-
},
96+
"metadata": {},
10197
"outputs": [
10298
{
10399
"data": {
104100
"text/html": [
105-
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~chelsea_lyn/7903.embed\" height=\"525px\" width=\"100%\"></iframe>"
101+
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~AdamKulidjian/6696.embed\" height=\"525px\" width=\"100%\"></iframe>"
106102
],
107103
"text/plain": [
108104
"<plotly.tools.PlotlyDisplay object>"
@@ -160,20 +156,18 @@
160156
"cell_type": "markdown",
161157
"metadata": {},
162158
"source": [
163-
"### Choropleth Inset Map"
159+
"#### Choropleth Inset Map"
164160
]
165161
},
166162
{
167163
"cell_type": "code",
168164
"execution_count": 3,
169-
"metadata": {
170-
"collapsed": false
171-
},
165+
"metadata": {},
172166
"outputs": [
173167
{
174168
"data": {
175169
"text/html": [
176-
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~chelsea_lyn/13235.embed\" height=\"525px\" width=\"100%\"></iframe>"
170+
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~AdamKulidjian/6698.embed\" height=\"525px\" width=\"100%\"></iframe>"
177171
],
178172
"text/plain": [
179173
"<plotly.tools.PlotlyDisplay object>"
@@ -276,136 +270,81 @@
276270
")\n",
277271
"\n",
278272
"fig = go.Figure(layout=layout, data=cases+inset)\n",
279-
"py.iplot(fig, validate=False, filename='West Africa Ebola cases 2014')\n"
273+
"py.iplot(fig, validate=False, filename='West Africa Ebola cases 2014')"
280274
]
281275
},
282276
{
283277
"cell_type": "markdown",
284278
"metadata": {},
285279
"source": [
286-
"### PACE Approved Legislation "
280+
"#### Full County Choropleths\n",
281+
"For the full county choropleth doc page checkout https://plot.ly/python/county-choropleth/"
287282
]
288283
},
289284
{
290285
"cell_type": "code",
291-
"execution_count": 1,
292-
"metadata": {
293-
"collapsed": false
294-
},
286+
"execution_count": 4,
287+
"metadata": {},
295288
"outputs": [
289+
{
290+
"name": "stdout",
291+
"output_type": "stream",
292+
"text": [
293+
"The draw time for this plot will be slow for clients without much RAM.\n"
294+
]
295+
},
296+
{
297+
"name": "stderr",
298+
"output_type": "stream",
299+
"text": [
300+
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/plotly/api/v1/clientresp.py:40: UserWarning:\n",
301+
"\n",
302+
"Estimated Draw Time Slow\n",
303+
"\n"
304+
]
305+
},
296306
{
297307
"data": {
298308
"text/html": [
299-
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~Diksha_Gabha/2513.embed\" height=\"525px\" width=\"800px\"></iframe>"
309+
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~AdamKulidjian/6672.embed\" height=\"450px\" width=\"900px\"></iframe>"
300310
],
301311
"text/plain": [
302312
"<plotly.tools.PlotlyDisplay object>"
303313
]
304314
},
305-
"execution_count": 1,
315+
"execution_count": 4,
306316
"metadata": {},
307317
"output_type": "execute_result"
308318
}
309319
],
310320
"source": [
311321
"import plotly.plotly as py\n",
312-
"from plotly.graph_objs import *\n",
322+
"import plotly.figure_factory as ff\n",
313323
"\n",
314-
"trace1 = Choropleth(\n",
315-
" z=['1', '1', '1', '1', '1', '1', '1'],\n",
316-
" autocolorscale=False,\n",
317-
" colorscale=[[0, 'rgb(255,255,255)'], [1, 'rgb(186,58,51)']],\n",
318-
" hoverinfo='text',\n",
319-
" locationmode='USA-states',\n",
320-
" locations=['AR', 'GA', 'KY', 'MO', 'UT', 'TX', 'WY'],\n",
321-
" name='Republican',\n",
322-
" showscale=False,\n",
323-
" text=['Arkansas', 'Georgia', 'Kentucky', 'Missouri', 'Utah', 'Texas', 'Wyoming'],\n",
324-
" zauto=False,\n",
325-
" zmax=1,\n",
326-
" zmin=0,\n",
327-
")\n",
328-
"trace2 = Choropleth(\n",
329-
" z=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],\n",
330-
" autocolorscale=False,\n",
331-
" colorscale=[[0, 'rgb(255,255,255)'], [1, 'rgb(68,94,150)']],\n",
332-
" hoverinfo='text',\n",
333-
" locationmode='USA-states',\n",
334-
" locations=['CA', 'CI', 'DOC', 'IL', 'MD', 'NJ', 'NM', 'NY', 'OR', 'RI', 'VT'],\n",
335-
" name='Democrat',\n",
336-
" showscale=False,\n",
337-
" text=['California', 'Connecticut', 'District of Columbia', 'Illinois', \n",
338-
" 'Maryland', 'New Jersey', 'New Mexico', 'New York', 'Oregon',\n",
339-
" 'Rhode Island', 'Vermont'],\n",
340-
" zauto=False,\n",
341-
" zmax=1,\n",
342-
" zmin=0,\n",
343-
")\n",
344-
"trace3 = Choropleth(\n",
345-
" z=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],\n",
346-
" autocolorscale=False,\n",
347-
" colorscale=[[0, 'rgb(255, 255, 255)'], [1, 'rgb(187, 170, 144)']],\n",
348-
" hoverinfo='text',\n",
349-
" locationmode='USA-states',\n",
350-
" locations=['CO', 'FL', 'MI', 'MN', 'NH', 'OH', 'VA', 'WI'],\n",
351-
" name='Swing State',\n",
352-
" showscale=False,\n",
353-
" text=['Colorado', 'Florida', 'Michigan', 'Minnesota', \n",
354-
" 'New Hampshire', 'Ohio', 'Virginia', 'Wisconsin'],\n",
355-
" zauto=False,\n",
356-
" zmax=1,\n",
357-
" zmin=0,\n",
358-
")\n",
324+
"import numpy as np\n",
325+
"import pandas as pd\n",
359326
"\n",
360-
"data = Data([trace1, trace2, trace3])\n",
361-
"layout = Layout(\n",
362-
" autosize=False,\n",
363-
" geo=dict(\n",
364-
" countrycolor='rgb(102, 102, 102)',\n",
365-
" countrywidth=0.1,\n",
366-
" lakecolor='rgb(255, 255, 255)',\n",
367-
" landcolor='rgba(237, 247, 138, 0.28)',\n",
368-
" lonaxis=dict(\n",
369-
" gridwidth=1.5999999999999999,\n",
370-
" range=[-180, -50],\n",
371-
" showgrid=False\n",
372-
" ),\n",
373-
" projection=dict(\n",
374-
" type='albers usa'\n",
375-
" ),\n",
376-
" scope='usa',\n",
377-
" showland=True,\n",
378-
" showrivers=False,\n",
379-
" showsubunits=True,\n",
380-
" subunitcolor='rgb(102, 102, 102)',\n",
381-
" subunitwidth=0.5\n",
382-
" ),\n",
383-
" hovermode='closest',\n",
384-
" images=list([\n",
385-
" dict(\n",
386-
" x=1,\n",
387-
" y=0.6,\n",
388-
" sizex=0.155,\n",
389-
" sizey=0.4,\n",
390-
" source='http://i.imgur.com/Xe3f1zg.png',\n",
391-
" xanchor='right',\n",
392-
" xref='paper',\n",
393-
" yanchor='bottom',\n",
394-
" yref='paper'\n",
395-
" )\n",
396-
" ]),\n",
397-
" showlegend=True,\n",
398-
" title='<b>PACE Approved legislation</b>',\n",
399-
" width= 800,\n",
400-
" margin = dict(\n",
401-
" l=0,\n",
402-
" r=50,\n",
403-
" b=100,\n",
404-
" t=100,\n",
405-
" pad=4)\n",
327+
"df_sample = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/laucnty16.csv')\n",
328+
"df_sample['State FIPS Code'] = df_sample['State FIPS Code'].apply(lambda x: str(x).zfill(2))\n",
329+
"df_sample['County FIPS Code'] = df_sample['County FIPS Code'].apply(lambda x: str(x).zfill(3))\n",
330+
"df_sample['FIPS'] = df_sample['State FIPS Code'] + df_sample['County FIPS Code']\n",
331+
"\n",
332+
"colorscale = [\"#f7fbff\",\"#ebf3fb\",\"#deebf7\",\"#d2e3f3\",\"#c6dbef\",\"#b3d2e9\",\"#9ecae1\",\n",
333+
" \"#85bcdb\",\"#6baed6\",\"#57a0ce\",\"#4292c6\",\"#3082be\",\"#2171b5\",\"#1361a9\",\n",
334+
" \"#08519c\",\"#0b4083\",\"#08306b\"]\n",
335+
"endpts = list(np.linspace(1, 12, len(colorscale) - 1))\n",
336+
"fips = df_sample['FIPS'].tolist()\n",
337+
"values = df_sample['Unemployment Rate (%)'].tolist()\n",
338+
"\n",
339+
"fig = ff.create_choropleth(\n",
340+
" fips=fips, values=values, scope=['usa'],\n",
341+
" binning_endpoints=endpts, colorscale=colorscale,\n",
342+
" show_state_data=False,\n",
343+
" show_hover=True, centroid_marker={'opacity': 0},\n",
344+
" asp=2.9, title='USA by Unemployment %',\n",
345+
" legend_title='% unemployed'\n",
406346
")\n",
407-
"fig = Figure(data=data, layout=layout)\n",
408-
"py.iplot(fig, filename='pace')"
347+
"py.iplot(fig, filename='choropleth_full_usa')"
409348
]
410349
},
411350
{
@@ -418,14 +357,8 @@
418357
},
419358
{
420359
"cell_type": "code",
421-
<<<<<<< HEAD
422360
"execution_count": 1,
423-
=======
424-
"execution_count": 2,
425-
>>>>>>> b22a31ab6cd91d151ab51741f3fcb448574008a5
426-
"metadata": {
427-
"collapsed": false
428-
},
361+
"metadata": {},
429362
"outputs": [
430363
{
431364
"data": {
@@ -456,58 +389,22 @@
456389
"output_type": "stream",
457390
"text": [
458391
"Collecting git+https://github.com/plotly/publisher.git\n",
459-
<<<<<<< HEAD
460-
" Cloning https://github.com/plotly/publisher.git to /var/folders/ld/6cl3s_l50wd40tdjq2b03jxh0000gp/T/pip-k8JnpP-build\n",
392+
" Cloning https://github.com/plotly/publisher.git to /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-OKLJ7r-build\n",
461393
"Installing collected packages: publisher\n",
462394
" Found existing installation: publisher 0.11\n",
463395
" Uninstalling publisher-0.11:\n",
464396
" Successfully uninstalled publisher-0.11\n",
465-
" Running setup.py install for publisher ... \u001b[?25l-\b \b\\\b \bdone\n",
466-
"\u001b[?25hSuccessfully installed publisher-0.10\n"
467-
=======
468-
" Cloning https://github.com/plotly/publisher.git to /tmp/pip-ittp0v-build\n",
469-
"Installing collected packages: publisher\n",
470-
" Running setup.py install for publisher ... \u001b[?25l-\b \berror\n",
471-
" Complete output from command /usr/bin/python -u -c \"import setuptools, tokenize;__file__='/tmp/pip-ittp0v-build/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\\r\\n', '\\n'), __file__, 'exec'))\" install --record /tmp/pip-lGFm1Y-record/install-record.txt --single-version-externally-managed --compile:\n",
472-
" running install\n",
473-
" running build\n",
474-
" running build_py\n",
475-
" creating build\n",
476-
" creating build/lib.linux-x86_64-2.7\n",
477-
" creating build/lib.linux-x86_64-2.7/publisher\n",
478-
" copying publisher/publisher.py -> build/lib.linux-x86_64-2.7/publisher\n",
479-
" copying publisher/__init__.py -> build/lib.linux-x86_64-2.7/publisher\n",
480-
" running install_lib\n",
481-
" creating /usr/local/lib/python2.7/dist-packages/publisher\n",
482-
" error: could not create '/usr/local/lib/python2.7/dist-packages/publisher': Permission denied\n",
483-
" \n",
484-
" ----------------------------------------\n",
485-
"\u001b[31mCommand \"/usr/bin/python -u -c \"import setuptools, tokenize;__file__='/tmp/pip-ittp0v-build/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\\r\\n', '\\n'), __file__, 'exec'))\" install --record /tmp/pip-lGFm1Y-record/install-record.txt --single-version-externally-managed --compile\" failed with error code 1 in /tmp/pip-ittp0v-build/\u001b[0m\n",
486-
"\u001b[?25h"
487-
]
488-
},
489-
{
490-
"name": "stderr",
491-
"output_type": "stream",
492-
"text": [
493-
"/home/diksha/anaconda2/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning:\n",
494-
"\n",
495-
"The `IPython.nbconvert` package has been deprecated. You should import from nbconvert instead.\n",
496-
"\n",
497-
"/home/diksha/anaconda2/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning:\n",
498-
"\n",
499-
"Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
500-
"\n"
501-
>>>>>>> b22a31ab6cd91d151ab51741f3fcb448574008a5
397+
" Running setup.py install for publisher ... \u001b[?25ldone\n",
398+
"\u001b[?25hSuccessfully installed publisher-0.11\n"
502399
]
503400
},
504401
{
505402
"name": "stderr",
506403
"output_type": "stream",
507404
"text": [
508-
"/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated. You should import from nbconvert instead.\n",
405+
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n",
509406
" \"You should import from nbconvert instead.\", ShimWarning)\n",
510-
"/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning: Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
407+
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning: Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
511408
" warnings.warn('Did you \"Save\" this notebook before running this command? '\n"
512409
]
513410
}
@@ -521,14 +418,13 @@
521418
"! pip install git+https://github.com/plotly/publisher.git --upgrade\n",
522419
"import publisher\n",
523420
"publisher.publish(\n",
524-
" 'Choropleth_maps.ipynb', 'python/choropleth-maps/', 'Choropleth Maps | plotly',\n",
525-
" 'How to make Choropleth maps in Python with Plotly.',\n",
526-
" title = 'Choropleth Maps | plotly',\n",
527-
" name = 'Choropleth Maps',\n",
421+
" 'Choropleth_maps.ipynb', 'python/choropleth-maps/', 'Choropleth Maps',\n",
422+
" 'How to make choropleth maps in Python with Plotly.',\n",
423+
" title = 'Python Choropleth Maps | Plotly',\n",
528424
" has_thumbnail='true', thumbnail='thumbnail/choropleth.jpg', \n",
529-
" language='python', page_type='example_index',\n",
530-
" display_as='maps', order=1,\n",
531-
" ipynb= '~notebook_demo/55')"
425+
" language='python',\n",
426+
" display_as='maps', order=1, ipynb='~notebook_demo/55',\n",
427+
" uses_plotly_offline=False)"
532428
]
533429
},
534430
{
@@ -542,7 +438,6 @@
542438
}
543439
],
544440
"metadata": {
545-
"anaconda-cloud": {},
546441
"kernelspec": {
547442
"display_name": "Python 2",
548443
"language": "python",
@@ -558,9 +453,9 @@
558453
"name": "python",
559454
"nbconvert_exporter": "python",
560455
"pygments_lexer": "ipython2",
561-
"version": "2.7.10"
456+
"version": "2.7.12"
562457
}
563458
},
564459
"nbformat": 4,
565-
"nbformat_minor": 0
460+
"nbformat_minor": 1
566461
}

0 commit comments

Comments
 (0)