--- jupyter: jupytext: notebook_metadata_filter: all text_representation: extension: .md format_name: markdown format_version: '1.3' jupytext_version: 1.14.1 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.8.8 plotly: description: How to make a Mapbox Choropleth Map of US Counties in Python with Plotly. display_as: maps language: python layout: base name: Mapbox Choropleth Maps order: 1 page_type: example_index permalink: python/mapbox-county-choropleth/ thumbnail: thumbnail/mapbox-choropleth.png --- A [Choropleth Map](https://en.wikipedia.org/wiki/Choropleth_map) is a map composed of colored polygons. It is used to represent spatial variations of a quantity. This page documents how to build **tile-map** choropleth maps, but you can also build [**outline** choropleth maps using our non-Mapbox trace types](/python/choropleth-maps). Below we show how to create Choropleth Maps using either Plotly Express' `px.choropleth_mapbox` function or the lower-level `go.Choroplethmapbox` graph object. #### Mapbox Access Tokens and Base Map Configuration To plot on Mapbox maps with Plotly you _may_ need a Mapbox account and a public [Mapbox Access Token](https://www.mapbox.com/studio). See our [Mapbox Map Layers](/python/mapbox-layers/) documentation for more information. ### Introduction: main parameters for choropleth tile maps Making choropleth Mapbox maps requires two main types of input: 1. GeoJSON-formatted geometry information where each feature has either an `id` field or some identifying value in `properties`. 2. A list of values indexed by feature identifier. The GeoJSON data is passed to the `geojson` argument, and the data is passed into the `color` argument of `px.choropleth_mapbox` (`z` if using `graph_objects`), in the same order as the IDs are passed into the `location` argument. **Note** the `geojson` attribute can also be the URL to a GeoJSON file, which can speed up map rendering in certain cases. #### GeoJSON with `feature.id` Here we load a GeoJSON file containing the geometry information for US counties, where `feature.id` is a [FIPS code](https://en.wikipedia.org/wiki/FIPS_county_code). ```python from urllib.request import urlopen import json with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response: counties = json.load(response) counties["features"][0] ``` #### Data indexed by `id` Here we load unemployment data by county, also indexed by [FIPS code](https://en.wikipedia.org/wiki/FIPS_county_code). ```python import pandas as pd df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", dtype={"fips": str}) df.head() ``` ### Choropleth map using plotly.express and carto base map (no token needed) [Plotly Express](/python/plotly-express/) is the easy-to-use, high-level interface to Plotly, which [operates on a variety of types of data](/python/px-arguments/) and produces [easy-to-style figures](/python/styling-plotly-express/). With `px.choropleth_mapbox`, each row of the DataFrame is represented as a region of the choropleth. ```python from urllib.request import urlopen import json with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response: counties = json.load(response) import pandas as pd df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", dtype={"fips": str}) import plotly.express as px fig = px.choropleth_mapbox(df, geojson=counties, locations='fips', color='unemp', color_continuous_scale="Viridis", range_color=(0, 12), mapbox_style="carto-positron", zoom=3, center = {"lat": 37.0902, "lon": -95.7129}, opacity=0.5, labels={'unemp':'unemployment rate'} ) fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) fig.show() ``` ### Choropleth maps in Dash [Dash](https://plotly.com/dash/) is the best way to build analytical apps in Python using Plotly figures. To run the app below, run `pip install dash`, click "Download" to get the code and run `python app.py`. Get started with [the official Dash docs](https://dash.plotly.com/installation) and **learn how to effortlessly [style](https://plotly.com/dash/design-kit/) & [deploy](https://plotly.com/dash/app-manager/) apps like this with Dash Enterprise.** ```python hide_code=true from IPython.display import IFrame snippet_url = 'https://python-docs-dash-snippets.herokuapp.com/python-docs-dash-snippets/' IFrame(snippet_url + 'mapbox-county-choropleth', width='100%', height=1200) ```
Sign up for Dash Club → Free cheat sheets plus updates from Chris Parmer and Adam Schroeder delivered to your inbox every two months. Includes tips and tricks, community apps, and deep dives into the Dash architecture. Join now.