Skip to content

Commit 608d114

Browse files
committed
update choropleth for maplibre changes
1 parent 76f6756 commit 608d114

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

doc/python/mapbox-county-choropleth.md

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.3'
9-
jupytext_version: 1.14.1
9+
jupytext_version: 1.16.3
1010
kernelspec:
11-
display_name: Python 3
11+
display_name: Python 3 (ipykernel)
1212
language: python
1313
name: python3
1414
language_info:
@@ -20,36 +20,32 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.8.8
23+
version: 3.10.0
2424
plotly:
25-
description: How to make a Mapbox Choropleth Map of US Counties in Python with
26-
Plotly.
25+
description: How to make a choropleth map of US counties in Python with Plotly.
2726
display_as: maps
2827
language: python
2928
layout: base
30-
name: Mapbox Choropleth Maps
29+
name: Tile Choropleth Maps
3130
order: 1
3231
page_type: example_index
33-
permalink: python/mapbox-county-choropleth/
32+
permalink: python/tile-county-choropleth/
33+
redirect_from: python/mapbox-county-choropleth/
3434
thumbnail: thumbnail/mapbox-choropleth.png
3535
---
3636

37-
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).
37+
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](/python/choropleth-maps).
3838

39-
Below we show how to create Choropleth Maps using either Plotly Express' `px.choropleth_mapbox` function or the lower-level `go.Choroplethmapbox` graph object.
40-
41-
#### Mapbox Access Tokens and Base Map Configuration
42-
43-
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.
39+
Below we show how to create Choropleth Maps using either Plotly Express' `px.choropleth_map` function or the lower-level `go.Choroplethmap` graph object.
4440

4541
### Introduction: main parameters for choropleth tile maps
4642

47-
Making choropleth Mapbox maps requires two main types of input:
43+
Making choropleth maps requires two main types of input:
4844

4945
1. GeoJSON-formatted geometry information where each feature has either an `id` field or some identifying value in `properties`.
5046
2. A list of values indexed by feature identifier.
5147

52-
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.
48+
The GeoJSON data is passed to the `geojson` argument, and the data is passed into the `color` argument of `px.choropleth_map` (`z` if using `graph_objects`), in the same order as the IDs are passed into the `location` argument.
5349

5450
**Note** the `geojson` attribute can also be the URL to a GeoJSON file, which can speed up map rendering in certain cases.
5551

@@ -77,11 +73,11 @@ df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-
7773
df.head()
7874
```
7975

80-
### Choropleth map using plotly.express and carto base map (no token needed)
76+
### Choropleth map using plotly.express and carto base map
8177

8278
[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/).
8379

84-
With `px.choropleth_mapbox`, each row of the DataFrame is represented as a region of the choropleth.
80+
With `px.choropleth_map`, each row of the DataFrame is represented as a region of the choropleth.
8581

8682
```python
8783
from urllib.request import urlopen
@@ -95,10 +91,10 @@ df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-
9591

9692
import plotly.express as px
9793

98-
fig = px.choropleth_mapbox(df, geojson=counties, locations='fips', color='unemp',
94+
fig = px.choropleth_map(df, geojson=counties, locations='fips', color='unemp',
9995
color_continuous_scale="Viridis",
10096
range_color=(0, 12),
101-
mapbox_style="carto-positron",
97+
map_style="carto-positron",
10298
zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
10399
opacity=0.5,
104100
labels={'unemp':'unemployment rate'}
@@ -148,10 +144,10 @@ import plotly.express as px
148144
df = px.data.election()
149145
geojson = px.data.election_geojson()
150146

151-
fig = px.choropleth_mapbox(df, geojson=geojson, color="Bergeron",
147+
fig = px.choropleth_map(df, geojson=geojson, color="Bergeron",
152148
locations="district", featureidkey="properties.district",
153149
center={"lat": 45.5517, "lon": -73.7073},
154-
mapbox_style="carto-positron", zoom=9)
150+
map_style="carto-positron", zoom=9)
155151
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
156152
fig.show()
157153
```
@@ -166,17 +162,17 @@ import plotly.express as px
166162
df = px.data.election()
167163
geojson = px.data.election_geojson()
168164

169-
fig = px.choropleth_mapbox(df, geojson=geojson, color="winner",
165+
fig = px.choropleth_map(df, geojson=geojson, color="winner",
170166
locations="district", featureidkey="properties.district",
171167
center={"lat": 45.5517, "lon": -73.7073},
172-
mapbox_style="carto-positron", zoom=9)
168+
map_style="carto-positron", zoom=9)
173169
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
174170
fig.show()
175171
```
176172

177173
### Using GeoPandas Data Frames
178174

179-
`px.choropleth_mapbox` accepts the `geometry` of a [GeoPandas](https://geopandas.org/) data frame as the input to `geojson` if the `geometry` contains polygons.
175+
`px.choropleth_map` accepts the `geometry` of a [GeoPandas](https://geopandas.org/) data frame as the input to `geojson` if the `geometry` contains polygons.
180176

181177
```python
182178
import plotly.express as px
@@ -187,19 +183,19 @@ geo_df = gpd.GeoDataFrame.from_features(
187183
px.data.election_geojson()["features"]
188184
).merge(df, on="district").set_index("district")
189185

190-
fig = px.choropleth_mapbox(geo_df,
186+
fig = px.choropleth_map(geo_df,
191187
geojson=geo_df.geometry,
192188
locations=geo_df.index,
193189
color="Joly",
194190
center={"lat": 45.5517, "lon": -73.7073},
195-
mapbox_style="open-street-map",
191+
map_style="open-street-map",
196192
zoom=8.5)
197193
fig.show()
198194
```
199195

200-
### Choropleth map using plotly.graph_objects and carto base map (no token needed)
196+
### Choropleth map using plotly.graph_objects and carto base map
201197

202-
If Plotly Express does not provide a good starting point, it is also possible to use [the more generic `go.Choroplethmapbox` class from `plotly.graph_objects`](/python/graph-objects/).
198+
If Plotly Express does not provide a good starting point, it is also possible to use [the more generic `go.Choroplethmap` class from `plotly.graph_objects`](/python/graph-objects/).
203199

204200
```python
205201
from urllib.request import urlopen
@@ -213,21 +209,26 @@ df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-
213209

214210
import plotly.graph_objects as go
215211

216-
fig = go.Figure(go.Choroplethmapbox(geojson=counties, locations=df.fips, z=df.unemp,
212+
fig = go.Figure(go.Choroplethmap(geojson=counties, locations=df.fips, z=df.unemp,
217213
colorscale="Viridis", zmin=0, zmax=12,
218214
marker_opacity=0.5, marker_line_width=0))
219-
fig.update_layout(mapbox_style="carto-positron",
220-
mapbox_zoom=3, mapbox_center = {"lat": 37.0902, "lon": -95.7129})
215+
fig.update_layout(map_style="carto-positron",
216+
map_zoom=3, map_center = {"lat": 37.0902, "lon": -95.7129})
221217
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
222218
fig.show()
223219
```
224220

225-
#### Mapbox Light base map: free token needed
221+
### Mapbox Maps
222+
223+
The earlier examples using `px.choropleth_map` and `go.Choroplethmap` use Maplibre for rendering. These traces were introduced in Plotly.py 5.24. These trace types are now the recommended way to create tile-based choropleth maps. There are also choropleth traces that use Mapbox: `px.choropleth_mapbox` and `go.Choroplethmapbox`
224+
225+
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.
226+
227+
Here's an exmaple of using the Mapbox Light base map, which requires a free token.
226228

227229
```python
228230
token = open(".mapbox_token").read() # you will need your own token
229231

230-
231232
from urllib.request import urlopen
232233
import json
233234
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
@@ -249,5 +250,6 @@ fig.show()
249250

250251
#### Reference
251252

252-
See [function reference for `px.(choropleth_mapbox)`](https://plotly.com/python-api-reference/generated/plotly.express.choropleth_mapbox) or https://plotly.com/python/reference/choroplethmapbox/ for more information about mapbox and their attribute options.
253+
See [function reference for `px.(choropleth_map)`](https://plotly.com/python-api-reference/generated/plotly.express.choropleth_map) or https://plotly.com/python/reference/choroplethmap/ for more information about the attributes available.
253254

255+
For Mapbox-based tile maps, see [function reference for `px.(choropleth_mapbox)`](https://plotly.com/python-api-reference/generated/plotly.express.choropleth_mapbox) or https://plotly.com/python/reference/choroplethmapbox/.

0 commit comments

Comments
 (0)