Skip to content

Commit 28f4813

Browse files
tweaks
1 parent 03bae47 commit 28f4813

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

python/mapbox-county-choropleth.md

+31-4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,33 @@ jupyter:
4343
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.
4444

4545

46+
Making choropleth maps with `go.Choroplethmapbox` requires two main types of input: GeoJSON-formatted geometry information *where each `feature` has an `id`* and a list of values indexed by feature id. The GeoJSON data is passed to the `geojson` attribute, and the data is passed into the `z` attribute, in the same order as the IDs are passed into the `location` attribute.
47+
48+
49+
#### GeoJSON with `feature.id`
50+
51+
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).
52+
53+
```python
54+
from urllib.request import urlopen
55+
import json
56+
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
57+
counties = json.load(response)
58+
59+
counties["features"][0]
60+
```
61+
62+
#### Data indexed by `id`
63+
64+
Here we load unemployment data by county, also indexed by [FIPS code](https://en.wikipedia.org/wiki/FIPS_county_code).
65+
66+
```python
67+
import pandas as pd
68+
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
69+
dtype={"fips": str})
70+
df.head()
71+
```
72+
4673
#### Carto base map: no token needed
4774

4875
```python
@@ -52,12 +79,12 @@ with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-c
5279
counties = json.load(response)
5380

5481
import pandas as pd
55-
unemp = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
82+
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
5683
dtype={"fips": str})
5784

5885
import plotly.graph_objects as go
5986

60-
fig = go.Figure(go.Choroplethmapbox(geojson=counties, locations=unemp.fips, z=unemp.unemp,
87+
fig = go.Figure(go.Choroplethmapbox(geojson=counties, locations=df.fips, z=df.unemp,
6188
colorscale="Viridis", zmin=0, zmax=12,
6289
marker_opacity=0.5, marker_line_width=0))
6390
fig.update_layout(mapbox_style="carto-positron",
@@ -78,12 +105,12 @@ with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-c
78105
counties = json.load(response)
79106

80107
import pandas as pd
81-
unemp = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
108+
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
82109
dtype={"fips": str})
83110

84111
import plotly.graph_objects as go
85112

86-
fig = go.Figure(go.Choroplethmapbox(geojson=counties, locations=unemp.fips, z=unemp.unemp,
113+
fig = go.Figure(go.Choroplethmapbox(geojson=counties, locations=df.fips, z=df.unemp,
87114
colorscale="Viridis", zmin=0, zmax=12, marker_line_width=0))
88115
fig.update_layout(mapbox_style="light", mapbox_accesstoken=token,
89116
mapbox_zoom=3, mapbox_center = {"lat": 37.0902, "lon": -95.7129})

0 commit comments

Comments
 (0)