{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### New to Plotly?\n",
"Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n",
" You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n",
" We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Version Check\n",
"Note: `United States County Choropleths` are available in version 2.5.1+ \n",
"Run `pip install plotly --upgrade` to update your Plotly version"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'3.6.1'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Required Packages\n",
"`geopandas`, `pyshp` and `shapely` must be installed for this figure factory.\n",
"\n",
"Run the following commands to install the correct versions of the following modules:\n",
"\n",
"```\n",
"pip install geopandas==0.3.0\n",
"pip install pyshp==1.2.10\n",
"pip install shapely==1.6.3\n",
"```\n",
"\n",
"If you are using Windows, follow this post to properly install geopandas and dependencies: http://geoffboeing.com/2014/09/using-geopandas-windows/. If you are using Anaconda, do not use PIP to install the packages above. Instead use conda to install them:\n",
"\n",
"```\n",
"conda install plotly\n",
"conda install geopandas\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### FIPS and Values\n",
"Every US state and county has an assined ID regulated by the US Federal Government under the term FIPS (Federal Information Processing Standards) codes. There are state codes and county codes: the 2016 state and county FIPS codes can be found at the [US Census Website](https://www.census.gov/geographies/reference-files/2016/demo/popest/2016-fips.html).\n",
"\n",
"Combine a state FIPS code (eg. `06` for California) with a county FIPS code of the state (eg. `059` for Orange county) and this new state-county FIPS code (`06059`) uniquely refers to the specified state and county.\n",
"\n",
"`ff.create_choropleth` only needs a list of FIPS codes and a list of values. Each FIPS code points to one county and each corresponding value in `values` determines the color of the county."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Simple Example\n",
"A simple example of this is a choropleth a few counties in California:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.figure_factory as ff\n",
"\n",
"fips = ['06021', '06023', '06027',\n",
" '06029', '06033', '06059',\n",
" '06047', '06049', '06051',\n",
" '06055', '06061']\n",
"values = range(len(fips))\n",
"\n",
"fig = ff.create_choropleth(fips=fips, values=values)\n",
"py.iplot(fig, filename='choropleth of some cali counties - full usa scope')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Change the Scope\n",
"Even if your FIPS values belong to a single state, the scope defaults to the entire United States as displayed in the example above. Changing the scope of the choropleth shifts the zoom and position of the USA map. You can define the scope with a list of state names and the zoom will automatically adjust to include the state outlines of the selected states.\n",
"\n",
"By default `scope` is set to `['USA']` which the API treats as identical to passing a list of all 50 state names: \n",
"\n",
"`['AK', 'AL', 'CA', ...]`\n",
"\n",
"State abbreviations (eg. `CA`) or the proper names (eg. `California`) as strings are accepted. If the state name is not recognized, the API will throw a Warning and indicate which FIPS values were ignored.\n",
"\n",
"Another param used in the example below is `binning_endpoints`. If your `values` is a list of numbers, you can bin your values into half-open intervals on the real line."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.figure_factory as ff\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"df_sample = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv')\n",
"df_sample_r = df_sample[df_sample['STNAME'] == 'California']\n",
"\n",
"values = df_sample_r['TOT_POP'].tolist()\n",
"fips = df_sample_r['FIPS'].tolist()\n",
"\n",
"colorscale = [\n",
" 'rgb(193, 193, 193)',\n",
" 'rgb(239,239,239)',\n",
" 'rgb(195, 196, 222)',\n",
" 'rgb(144,148,194)',\n",
" 'rgb(101,104,168)',\n",
" 'rgb(65, 53, 132)'\n",
"]\n",
"\n",
"fig = ff.create_choropleth(\n",
" fips=fips, values=values, scope=['CA', 'AZ', 'Nevada', 'Oregon', ' Idaho'],\n",
" binning_endpoints=[14348, 63983, 134827, 426762, 2081313], colorscale=colorscale,\n",
" county_outline={'color': 'rgb(255,255,255)', 'width': 0.5}, round_legend_values=True,\n",
" legend_title='Population by County', title='California and Nearby States'\n",
")\n",
"py.iplot(fig, filename='choropleth_california_and_surr_states_outlines')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Single State"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.figure_factory as ff\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"df_sample = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv')\n",
"df_sample_r = df_sample[df_sample['STNAME'] == 'Florida']\n",
"\n",
"values = df_sample_r['TOT_POP'].tolist()\n",
"fips = df_sample_r['FIPS'].tolist()\n",
"\n",
"endpts = list(np.mgrid[min(values):max(values):4j])\n",
"colorscale = [\"#030512\",\"#1d1d3b\",\"#323268\",\"#3d4b94\",\"#3e6ab0\",\n",
" \"#4989bc\",\"#60a7c7\",\"#85c5d3\",\"#b7e0e4\",\"#eafcfd\"]\n",
"fig = ff.create_choropleth(\n",
" fips=fips, values=values, scope=['Florida'], show_state_data=True,\n",
" colorscale=colorscale, binning_endpoints=endpts, round_legend_values=True,\n",
" plot_bgcolor='rgb(229,229,229)',\n",
" paper_bgcolor='rgb(229,229,229)',\n",
" legend_title='Population by County',\n",
" county_outline={'color': 'rgb(255,255,255)', 'width': 0.5},\n",
" exponent_format=True,\n",
")\n",
"py.iplot(fig, filename='choropleth_florida')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Multiple States"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.figure_factory as ff\n",
"\n",
"import pandas as pd\n",
"\n",
"NE_states = ['Connecticut', 'Maine', 'Massachusetts', 'New Hampshire', 'Rhode Island', 'Vermont']\n",
"df_sample = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv')\n",
"df_sample_r = df_sample[df_sample['STNAME'].isin(NE_states)]\n",
"\n",
"values = df_sample_r['TOT_POP'].tolist()\n",
"fips = df_sample_r['FIPS'].tolist()\n",
"\n",
"colorscale = [\n",
" 'rgb(68.0, 1.0, 84.0)',\n",
" 'rgb(66.0, 64.0, 134.0)',\n",
" 'rgb(38.0, 130.0, 142.0)',\n",
" 'rgb(63.0, 188.0, 115.0)',\n",
" 'rgb(216.0, 226.0, 25.0)'\n",
"]\n",
"\n",
"fig = ff.create_choropleth(\n",
" fips=fips, values=values,\n",
" scope=NE_states, county_outline={'color': 'rgb(255,255,255)', 'width': 0.5},\n",
" legend_title='Population per county'\n",
" \n",
")\n",
"fig['layout']['legend'].update({'x': 0})\n",
"fig['layout']['annotations'][0].update({'x': -0.12, 'xanchor': 'left'})\n",
"py.iplot(fig, filename='choropleth_new_england')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Simplify County, State Lines\n",
"Below is a choropleth that uses several other parameters. For a full list of all available params call `help(ff.create_choropleth)`\n",
"\n",
"- `simplify_county` determines the simplification factor for the counties. The larger the number, the fewer vertices and edges each polygon has. See http://toblerity.org/shapely/manual.html#object.simplify for more information.\n",
"- `simplify_state` simplifies the state outline polygon. See the [documentation](http://toblerity.org/shapely/manual.html#object.simplify) for more information.\n",
"Default for both `simplify_county` and `simplif_state` is 0.02\n",
"\n",
"Note: This choropleth uses a divergent categorical colorscale. See http://react-colorscales.getforge.io/ for other cool colorscales."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.figure_factory as ff\n",
"\n",
"import pandas as pd\n",
"\n",
"scope = ['Oregon']\n",
"df_sample = pd.read_csv(\n",
" 'https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv'\n",
")\n",
"df_sample_r = df_sample[df_sample['STNAME'].isin(scope)]\n",
"\n",
"values = df_sample_r['TOT_POP'].tolist()\n",
"fips = df_sample_r['FIPS'].tolist()\n",
"\n",
"colorscale = [\"#8dd3c7\", \"#ffffb3\", \"#bebada\", \"#fb8072\",\n",
" \"#80b1d3\", \"#fdb462\", \"#b3de69\", \"#fccde5\",\n",
" \"#d9d9d9\", \"#bc80bd\", \"#ccebc5\", \"#ffed6f\",\n",
" \"#8dd3c7\", \"#ffffb3\", \"#bebada\", \"#fb8072\",\n",
" \"#80b1d3\", \"#fdb462\", \"#b3de69\", \"#fccde5\",\n",
" \"#d9d9d9\", \"#bc80bd\", \"#ccebc5\", \"#ffed6f\",\n",
" \"#8dd3c7\", \"#ffffb3\", \"#bebada\", \"#fb8072\",\n",
" \"#80b1d3\", \"#fdb462\", \"#b3de69\", \"#fccde5\",\n",
" \"#d9d9d9\", \"#bc80bd\", \"#ccebc5\", \"#ffed6f\"]\n",
"\n",
"fig = ff.create_choropleth(\n",
" fips=fips, values=values, scope=scope,\n",
" colorscale=colorscale, round_legend_values=True,\n",
" simplify_county=0, simplify_state=0,\n",
" county_outline={'color': 'rgb(15, 15, 55)', 'width': 0.5},\n",
" state_outline={'width': 1},\n",
" legend_title='pop. per county',\n",
" title='Oregon'\n",
")\n",
"\n",
"py.iplot(fig, filename='choropleth_oregon_ono_simplification_factor')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The Entire USA"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The draw time for this plot will be slow for clients without much RAM.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/michael/.virtualenvs/plot2py2/local/lib/python2.7/site-packages/plotly/api/v1/clientresp.py:40: UserWarning:\n",
"\n",
"Estimated Draw Time Slow\n",
"\n"
]
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.figure_factory as ff\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"\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,\n",
" binning_endpoints=endpts,\n",
" 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",
"py.iplot(fig, filename='choropleth_full_usa')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Also see Mapbox county choropleths made in Python: [https://plotly.com/python/mapbox-county-choropleth/](https://plotly.com/python/mapbox-county-choropleth/)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function create_choropleth in module plotly.figure_factory._county_choropleth:\n",
"\n",
"create_choropleth(fips, values, scope=['usa'], binning_endpoints=None, colorscale=None, order=None, simplify_county=0.02, simplify_state=0.02, asp=None, show_hover=True, show_state_data=True, state_outline=None, county_outline=None, centroid_marker=None, round_legend_values=False, exponent_format=False, legend_title='', **layout_options)\n",
" Returns figure for county choropleth. Uses data from package_data.\n",
" \n",
" :param (list) fips: list of FIPS values which correspond to the con\n",
" catination of state and county ids. An example is '01001'.\n",
" :param (list) values: list of numbers/strings which correspond to the\n",
" fips list. These are the values that will determine how the counties\n",
" are colored.\n",
" :param (list) scope: list of states and/or states abbreviations. Fits\n",
" all states in the camera tightly. Selecting ['usa'] is the equivalent\n",
" of appending all 50 states into your scope list. Selecting only 'usa'\n",
" does not include 'Alaska', 'Puerto Rico', 'American Samoa',\n",
" 'Commonwealth of the Northern Mariana Islands', 'Guam',\n",
" 'United States Virgin Islands'. These must be added manually to the\n",
" list.\n",
" Default = ['usa']\n",
" :param (list) binning_endpoints: ascending numbers which implicitly define\n",
" real number intervals which are used as bins. The colorscale used must\n",
" have the same number of colors as the number of bins and this will\n",
" result in a categorical colormap.\n",
" :param (list) colorscale: a list of colors with length equal to the\n",
" number of categories of colors. The length must match either all\n",
" unique numbers in the 'values' list or if endpoints is being used, the\n",
" number of categories created by the endpoints.\n",
" \n",
" For example, if binning_endpoints = [4, 6, 8], then there are 4 bins:\n",
" [-inf, 4), [4, 6), [6, 8), [8, inf)\n",
" :param (list) order: a list of the unique categories (numbers/bins) in any\n",
" desired order. This is helpful if you want to order string values to\n",
" a chosen colorscale.\n",
" :param (float) simplify_county: determines the simplification factor\n",
" for the counties. The larger the number, the fewer vertices and edges\n",
" each polygon has. See\n",
" http://toblerity.org/shapely/manual.html#object.simplify for more\n",
" information.\n",
" Default = 0.02\n",
" :param (float) simplify_state: simplifies the state outline polygon.\n",
" See http://toblerity.org/shapely/manual.html#object.simplify for more\n",
" information.\n",
" Default = 0.02\n",
" :param (float) asp: the width-to-height aspect ratio for the camera.\n",
" Default = 2.5\n",
" :param (bool) show_hover: show county hover and centroid info\n",
" :param (bool) show_state_data: reveals state boundary lines\n",
" :param (dict) state_outline: dict of attributes of the state outline\n",
" including width and color. See\n",
" https://plotly.com/python/reference/#scatter-marker-line for all valid\n",
" params\n",
" :param (dict) county_outline: dict of attributes of the county outline\n",
" including width and color. See\n",
" https://plotly.com/python/reference/#scatter-marker-line for all valid\n",
" params\n",
" :param (dict) centroid_marker: dict of attributes of the centroid marker.\n",
" The centroid markers are invisible by default and appear visible on\n",
" selection. See https://plotly.com/python/reference/#scatter-marker for\n",
" all valid params\n",
" :param (bool) round_legend_values: automatically round the numbers that\n",
" appear in the legend to the nearest integer.\n",
" Default = False\n",
" :param (bool) exponent_format: if set to True, puts numbers in the K, M,\n",
" B number format. For example 4000.0 becomes 4.0K\n",
" Default = False\n",
" :param (str) legend_title: title that appears above the legend\n",
" :param **layout_options: a **kwargs argument for all layout parameters\n",
" \n",
" \n",
" Example 1: Florida\n",
" ```\n",
" import plotly.plotly as py\n",
" import plotly.figure_factory as ff\n",
" \n",
" import numpy as np\n",
" import pandas as pd\n",
" \n",
" df_sample = pd.read_csv(\n",
" 'https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv'\n",
" )\n",
" df_sample_r = df_sample[df_sample['STNAME'] == 'Florida']\n",
" \n",
" values = df_sample_r['TOT_POP'].tolist()\n",
" fips = df_sample_r['FIPS'].tolist()\n",
" \n",
" binning_endpoints = list(np.mgrid[min(values):max(values):4j])\n",
" colorscale = [\"#030512\",\"#1d1d3b\",\"#323268\",\"#3d4b94\",\"#3e6ab0\",\n",
" \"#4989bc\",\"#60a7c7\",\"#85c5d3\",\"#b7e0e4\",\"#eafcfd\"]\n",
" fig = ff.create_choropleth(\n",
" fips=fips, values=values, scope=['Florida'], show_state_data=True,\n",
" colorscale=colorscale, binning_endpoints=binning_endpoints,\n",
" round_legend_values=True, plot_bgcolor='rgb(229,229,229)',\n",
" paper_bgcolor='rgb(229,229,229)', legend_title='Florida Population',\n",
" county_outline={'color': 'rgb(255,255,255)', 'width': 0.5},\n",
" exponent_format=True,\n",
" )\n",
" py.iplot(fig, filename='choropleth_florida')\n",
" ```\n",
" \n",
" Example 2: New England\n",
" ```\n",
" import plotly.plotly as py\n",
" import plotly.figure_factory as ff\n",
" \n",
" import pandas as pd\n",
" \n",
" NE_states = ['Connecticut', 'Maine', 'Massachusetts',\n",
" 'New Hampshire', 'Rhode Island']\n",
" df_sample = pd.read_csv(\n",
" 'https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv'\n",
" )\n",
" df_sample_r = df_sample[df_sample['STNAME'].isin(NE_states)]\n",
" colorscale = ['rgb(68.0, 1.0, 84.0)',\n",
" 'rgb(66.0, 64.0, 134.0)',\n",
" 'rgb(38.0, 130.0, 142.0)',\n",
" 'rgb(63.0, 188.0, 115.0)',\n",
" 'rgb(216.0, 226.0, 25.0)']\n",
" \n",
" values = df_sample_r['TOT_POP'].tolist()\n",
" fips = df_sample_r['FIPS'].tolist()\n",
" fig = ff.create_choropleth(\n",
" fips=fips, values=values, scope=NE_states, show_state_data=True\n",
" )\n",
" py.iplot(fig, filename='choropleth_new_england')\n",
" ```\n",
" \n",
" Example 3: California and Surrounding States\n",
" ```\n",
" import plotly.plotly as py\n",
" import plotly.figure_factory as ff\n",
" \n",
" import pandas as pd\n",
" \n",
" df_sample = pd.read_csv(\n",
" 'https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv'\n",
" )\n",
" df_sample_r = df_sample[df_sample['STNAME'] == 'California']\n",
" \n",
" values = df_sample_r['TOT_POP'].tolist()\n",
" fips = df_sample_r['FIPS'].tolist()\n",
" \n",
" colorscale = [\n",
" 'rgb(193, 193, 193)',\n",
" 'rgb(239,239,239)',\n",
" 'rgb(195, 196, 222)',\n",
" 'rgb(144,148,194)',\n",
" 'rgb(101,104,168)',\n",
" 'rgb(65, 53, 132)'\n",
" ]\n",
" \n",
" fig = ff.create_choropleth(\n",
" fips=fips, values=values, colorscale=colorscale,\n",
" scope=['CA', 'AZ', 'Nevada', 'Oregon', ' Idaho'],\n",
" binning_endpoints=[14348, 63983, 134827, 426762, 2081313],\n",
" county_outline={'color': 'rgb(255,255,255)', 'width': 0.5},\n",
" legend_title='California Counties',\n",
" title='California and Nearby States'\n",
" )\n",
" py.iplot(fig, filename='choropleth_california_and_surr_states_outlines')\n",
" ```\n",
" \n",
" Example 4: USA\n",
" ```\n",
" import plotly.plotly as py\n",
" import plotly.figure_factory as ff\n",
" \n",
" import numpy as np\n",
" import pandas as pd\n",
" \n",
" df_sample = pd.read_csv(\n",
" 'https://raw.githubusercontent.com/plotly/datasets/master/laucnty16.csv'\n",
" )\n",
" df_sample['State FIPS Code'] = df_sample['State FIPS Code'].apply(\n",
" lambda x: str(x).zfill(2)\n",
" )\n",
" df_sample['County FIPS Code'] = df_sample['County FIPS Code'].apply(\n",
" lambda x: str(x).zfill(3)\n",
" )\n",
" df_sample['FIPS'] = (\n",
" df_sample['State FIPS Code'] + df_sample['County FIPS Code']\n",
" )\n",
" \n",
" binning_endpoints = list(np.linspace(1, 12, len(colorscale) - 1))\n",
" colorscale = [\"#f7fbff\", \"#ebf3fb\", \"#deebf7\", \"#d2e3f3\", \"#c6dbef\",\n",
" \"#b3d2e9\", \"#9ecae1\", \"#85bcdb\", \"#6baed6\", \"#57a0ce\",\n",
" \"#4292c6\", \"#3082be\", \"#2171b5\", \"#1361a9\", \"#08519c\",\n",
" \"#0b4083\",\"#08306b\"]\n",
" fips = df_sample['FIPS']\n",
" values = df_sample['Unemployment Rate (%)']\n",
" fig = ff.create_choropleth(\n",
" fips=fips, values=values, scope=['usa'],\n",
" binning_endpoints=binning_endpoints, colorscale=colorscale,\n",
" show_hover=True, centroid_marker={'opacity': 0},\n",
" asp=2.9, title='USA by Unemployment %',\n",
" legend_title='Unemployment %'\n",
" )\n",
" \n",
" py.iplot(fig, filename='choropleth_full_usa')\n",
" ```\n",
"\n"
]
}
],
"source": [
"help(ff.create_choropleth)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting git+https://github.com/plotly/publisher.git\n",
" Cloning https://github.com/plotly/publisher.git to /tmp/pip-req-build-h71Kdm\n",
"Building wheels for collected packages: publisher\n",
" Running setup.py bdist_wheel for publisher ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /tmp/pip-ephem-wheel-cache-37a0oz/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n",
"Successfully built publisher\n",
"Installing collected packages: publisher\n",
"Successfully installed publisher-0.11\n",
"\u001b[33mYou are using pip version 10.0.1, however version 18.0 is available.\n",
"You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/michael/.virtualenvs/plot2py2/local/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning:\n",
"\n",
"The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n",
"\n",
"/home/michael/.virtualenvs/plot2py2/local/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"
]
}
],
"source": [
"from IPython.display import display, HTML\n",
"\n",
"display(HTML(''))\n",
"display(HTML(''))\n",
"\n",
"! pip install git+https://github.com/plotly/publisher.git --upgrade\n",
"import publisher\n",
"publisher.publish(\n",
" 'county_choropleth.ipynb', 'python/county-choropleth/', 'USA County Choropleth Maps',\n",
" 'How to create colormaped representations of USA counties by FIPS values in Python.',\n",
" title = 'Python USA County Choropleth Maps | Plotly',\n",
" has_thumbnail='true', thumbnail='thumbnail/county-choropleth-usa-greybkgd.jpg', \n",
" language='python', page_type='example_index',\n",
" display_as='maps', order=0,\n",
" uses_plotly_offline=False,ipynb='~notebook_demo/212')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"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.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}