Skip to content

Choropleth Docs #879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 22, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 88 additions & 130 deletions _posts/python/maps/choropleth-maps/2015-06-30-Choropleth_maps.html

Large diffs are not rendered by default.

240 changes: 70 additions & 170 deletions _posts/python/maps/choropleth-maps/Choropleth_maps.ipynb
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~chelsea_lyn/7898.embed\" height=\"525px\" width=\"100%\"></iframe>"
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~AdamKulidjian/6694.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
Expand Down Expand Up @@ -95,14 +93,12 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~chelsea_lyn/7903.embed\" height=\"525px\" width=\"100%\"></iframe>"
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~AdamKulidjian/6696.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
Expand Down Expand Up @@ -166,14 +162,12 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~chelsea_lyn/13235.embed\" height=\"525px\" width=\"100%\"></iframe>"
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~AdamKulidjian/6698.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
Expand Down Expand Up @@ -276,136 +270,81 @@
")\n",
"\n",
"fig = go.Figure(layout=layout, data=cases+inset)\n",
"py.iplot(fig, validate=False, filename='West Africa Ebola cases 2014')\n"
"py.iplot(fig, validate=False, filename='West Africa Ebola cases 2014')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### PACE Approved Legislation "
"#### Full County Choropleths\n",
"For the full county choropleth doc page checkout https://plot.ly/python/county-choropleth/"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The draw time for this plot will be slow for clients without much RAM.\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this warning is ok

]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/plotly/api/v1/clientresp.py:40: UserWarning:\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove this warning from the docs?
(you can run the cell twice or run:

import warnings
warnings.filterwarnings('ignore')

in the beginning of your notebook )

"\n",
"Estimated Draw Time Slow\n",
"\n"
]
},
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~Diksha_Gabha/2513.embed\" height=\"525px\" width=\"800px\"></iframe>"
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~AdamKulidjian/6672.embed\" height=\"450px\" width=\"900px\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 1,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"from plotly.graph_objs import *\n",
"import plotly.figure_factory as ff\n",
"\n",
"trace1 = Choropleth(\n",
" z=['1', '1', '1', '1', '1', '1', '1'],\n",
" autocolorscale=False,\n",
" colorscale=[[0, 'rgb(255,255,255)'], [1, 'rgb(186,58,51)']],\n",
" hoverinfo='text',\n",
" locationmode='USA-states',\n",
" locations=['AR', 'GA', 'KY', 'MO', 'UT', 'TX', 'WY'],\n",
" name='Republican',\n",
" showscale=False,\n",
" text=['Arkansas', 'Georgia', 'Kentucky', 'Missouri', 'Utah', 'Texas', 'Wyoming'],\n",
" zauto=False,\n",
" zmax=1,\n",
" zmin=0,\n",
")\n",
"trace2 = Choropleth(\n",
" z=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],\n",
" autocolorscale=False,\n",
" colorscale=[[0, 'rgb(255,255,255)'], [1, 'rgb(68,94,150)']],\n",
" hoverinfo='text',\n",
" locationmode='USA-states',\n",
" locations=['CA', 'CI', 'DOC', 'IL', 'MD', 'NJ', 'NM', 'NY', 'OR', 'RI', 'VT'],\n",
" name='Democrat',\n",
" showscale=False,\n",
" text=['California', 'Connecticut', 'District of Columbia', 'Illinois', \n",
" 'Maryland', 'New Jersey', 'New Mexico', 'New York', 'Oregon',\n",
" 'Rhode Island', 'Vermont'],\n",
" zauto=False,\n",
" zmax=1,\n",
" zmin=0,\n",
")\n",
"trace3 = Choropleth(\n",
" z=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],\n",
" autocolorscale=False,\n",
" colorscale=[[0, 'rgb(255, 255, 255)'], [1, 'rgb(187, 170, 144)']],\n",
" hoverinfo='text',\n",
" locationmode='USA-states',\n",
" locations=['CO', 'FL', 'MI', 'MN', 'NH', 'OH', 'VA', 'WI'],\n",
" name='Swing State',\n",
" showscale=False,\n",
" text=['Colorado', 'Florida', 'Michigan', 'Minnesota', \n",
" 'New Hampshire', 'Ohio', 'Virginia', 'Wisconsin'],\n",
" zauto=False,\n",
" zmax=1,\n",
" zmin=0,\n",
")\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"data = Data([trace1, trace2, trace3])\n",
"layout = Layout(\n",
" autosize=False,\n",
" geo=dict(\n",
" countrycolor='rgb(102, 102, 102)',\n",
" countrywidth=0.1,\n",
" lakecolor='rgb(255, 255, 255)',\n",
" landcolor='rgba(237, 247, 138, 0.28)',\n",
" lonaxis=dict(\n",
" gridwidth=1.5999999999999999,\n",
" range=[-180, -50],\n",
" showgrid=False\n",
" ),\n",
" projection=dict(\n",
" type='albers usa'\n",
" ),\n",
" scope='usa',\n",
" showland=True,\n",
" showrivers=False,\n",
" showsubunits=True,\n",
" subunitcolor='rgb(102, 102, 102)',\n",
" subunitwidth=0.5\n",
" ),\n",
" hovermode='closest',\n",
" images=list([\n",
" dict(\n",
" x=1,\n",
" y=0.6,\n",
" sizex=0.155,\n",
" sizey=0.4,\n",
" source='http://i.imgur.com/Xe3f1zg.png',\n",
" xanchor='right',\n",
" xref='paper',\n",
" yanchor='bottom',\n",
" yref='paper'\n",
" )\n",
" ]),\n",
" showlegend=True,\n",
" title='<b>PACE Approved legislation</b>',\n",
" width= 800,\n",
" margin = dict(\n",
" l=0,\n",
" r=50,\n",
" b=100,\n",
" t=100,\n",
" pad=4)\n",
"df_sample = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/laucnty16.csv')\n",
"df_sample['State FIPS Code'] = df_sample['State FIPS Code'].apply(lambda x: str(x).zfill(2))\n",
"df_sample['County FIPS Code'] = df_sample['County FIPS Code'].apply(lambda x: str(x).zfill(3))\n",
"df_sample['FIPS'] = df_sample['State FIPS Code'] + df_sample['County FIPS Code']\n",
"\n",
"colorscale = [\"#f7fbff\",\"#ebf3fb\",\"#deebf7\",\"#d2e3f3\",\"#c6dbef\",\"#b3d2e9\",\"#9ecae1\",\n",
" \"#85bcdb\",\"#6baed6\",\"#57a0ce\",\"#4292c6\",\"#3082be\",\"#2171b5\",\"#1361a9\",\n",
" \"#08519c\",\"#0b4083\",\"#08306b\"]\n",
"endpts = list(np.linspace(1, 12, len(colorscale) - 1))\n",
"fips = df_sample['FIPS'].tolist()\n",
"values = df_sample['Unemployment Rate (%)'].tolist()\n",
"\n",
"fig = ff.create_choropleth(\n",
" fips=fips, values=values, scope=['usa'],\n",
" binning_endpoints=endpts, colorscale=colorscale,\n",
" show_state_data=False,\n",
" show_hover=True, centroid_marker={'opacity': 0},\n",
" asp=2.9, title='USA by Unemployment %',\n",
" legend_title='% unemployed'\n",
")\n",
"fig = Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='pace')"
"py.iplot(fig, filename='choropleth_full_usa')"
]
},
{
Expand All @@ -418,14 +357,8 @@
},
{
"cell_type": "code",
<<<<<<< HEAD
"execution_count": 1,
=======
"execution_count": 2,
>>>>>>> b22a31ab6cd91d151ab51741f3fcb448574008a5
"metadata": {
"collapsed": false
},
"execution_count": 5,
"metadata": {},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm looks like there's no reference section in this doc- can you add a reference section pointing to: https://plot.ly/python/reference/#choropleth

"outputs": [
{
"data": {
Expand Down Expand Up @@ -456,59 +389,27 @@
"output_type": "stream",
"text": [
"Collecting git+https://github.com/plotly/publisher.git\n",
<<<<<<< HEAD
" Cloning https://github.com/plotly/publisher.git to /var/folders/ld/6cl3s_l50wd40tdjq2b03jxh0000gp/T/pip-k8JnpP-build\n",
" Cloning https://github.com/plotly/publisher.git to /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-Qur0ve-build\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.11\n",
" Uninstalling publisher-0.11:\n",
" Successfully uninstalled publisher-0.11\n",
" Running setup.py install for publisher ... \u001b[?25l-\b \b\\\b \bdone\n",
"\u001b[?25hSuccessfully installed publisher-0.10\n"
=======
" Cloning https://github.com/plotly/publisher.git to /tmp/pip-ittp0v-build\n",
"Installing collected packages: publisher\n",
" Running setup.py install for publisher ... \u001b[?25l-\b \berror\n",
" 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",
" running install\n",
" running build\n",
" running build_py\n",
" creating build\n",
" creating build/lib.linux-x86_64-2.7\n",
" creating build/lib.linux-x86_64-2.7/publisher\n",
" copying publisher/publisher.py -> build/lib.linux-x86_64-2.7/publisher\n",
" copying publisher/__init__.py -> build/lib.linux-x86_64-2.7/publisher\n",
" running install_lib\n",
" creating /usr/local/lib/python2.7/dist-packages/publisher\n",
" error: could not create '/usr/local/lib/python2.7/dist-packages/publisher': Permission denied\n",
" \n",
" ----------------------------------------\n",
"\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",
"\u001b[?25h"
" Running setup.py install for publisher ... \u001b[?25ldone\n",
"\u001b[?25hSuccessfully installed publisher-0.11\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/diksha/anaconda2/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning:\n",
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning:\n",
"\n",
"The `IPython.nbconvert` package has been deprecated. You should import from nbconvert instead.\n",
"The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n",
"\n",
"/home/diksha/anaconda2/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning:\n",
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning:\n",
"\n",
"Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
"\n"
>>>>>>> b22a31ab6cd91d151ab51741f3fcb448574008a5
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/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",
" \"You should import from nbconvert instead.\", ShimWarning)\n",
"/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",
" warnings.warn('Did you \"Save\" this notebook before running this command? '\n"
]
}
],
Expand All @@ -521,14 +422,14 @@
"! pip install git+https://github.com/plotly/publisher.git --upgrade\n",
"import publisher\n",
"publisher.publish(\n",
" 'Choropleth_maps.ipynb', 'python/choropleth-maps/', 'Choropleth Maps | plotly',\n",
" 'How to make Choropleth maps in Python with Plotly.',\n",
" title = 'Choropleth Maps | plotly',\n",
" name = 'Choropleth Maps',\n",
" 'Choropleth_maps.ipynb', 'python/choropleth-maps/', 'Choropleth Maps',\n",
" 'How to make choropleth maps in Python with Plotly.',\n",
" title = 'Python Choropleth Maps | Plotly',\n",
" has_thumbnail='true', thumbnail='thumbnail/choropleth.jpg', \n",
" language='python', page_type='example_index',\n",
" display_as='maps', order=1,\n",
" ipynb= '~notebook_demo/55')"
" language='python', \n",
" page_type='example_index',\n",
" display_as='maps', order=1, ipynb='~notebook_demo/55',\n",
" uses_plotly_offline=False)"
]
},
{
Expand All @@ -542,7 +443,6 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 2",
"language": "python",
Expand All @@ -558,9 +458,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}
Loading