Skip to content

Commit 8e2538d

Browse files
committed
update map layers doc
1 parent b90f6ea commit 8e2538d

File tree

1 file changed

+168
-55
lines changed

1 file changed

+168
-55
lines changed

doc/python/mapbox-layers.md

Lines changed: 168 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.3'
9-
jupytext_version: 1.14.6
9+
jupytext_version: 1.16.3
1010
kernelspec:
1111
display_name: Python 3 (ipykernel)
1212
language: python
@@ -20,63 +20,83 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.10.11
23+
version: 3.10.0
2424
plotly:
25-
description: How to make Mapbox maps in Python with various base layers, with
26-
or without needing a Mapbox Access token.
25+
description: How to make tile-based maps in Python with various base layers.
2726
display_as: maps
2827
language: python
2928
layout: base
30-
name: Mapbox Map Layers
29+
name: Tile Map Layers
3130
order: 8
3231
page_type: u-guide
33-
permalink: python/mapbox-layers/
32+
permalink: /python/tile-map-layers/
33+
redirect_from: /python/mapbox-layers/
3434
thumbnail: thumbnail/mapbox-layers.png
3535
---
3636

3737
<!-- #region -->
3838

39-
### Mapbox Maps vs Geo Maps
39+
## Tile Maps vs Outline Maps
4040

4141
Plotly supports two different kinds of maps:
4242

43-
1. **Mapbox maps** are [tile-based maps](https://en.wikipedia.org/wiki/Tiled_web_map). If your figure is created with a `px.scatter_mapbox`, `px.line_mapbox`, `px.choropleth_mapbox` or `px.density_mapbox` function or otherwise contains one or more traces of type `go.Scattermapbox`, `go.Choroplethmapbox` or `go.Densitymapbox`, the `layout.mapbox` object in your figure contains configuration information for the map itself.
44-
2. **Geo maps** are outline-based maps. If your figure is created with a `px.scatter_geo`, `px.line_geo` or `px.choropleth` function or otherwise contains one or more traces of type `go.Scattergeo` or `go.Choropleth`, the `layout.geo` object in your figure contains configuration information for the map itself.
43+
- **[Tile-based maps](https://en.wikipedia.org/wiki/Tiled_web_map)**
4544

46-
This page documents Mapbox tile-based maps, and the [Geo map documentation](/python/map-configuration/) describes how to configure outline-based maps.
45+
If your figure is created with a `px.scatter_map`, `px_scatter_mapbox`, `px.line_map`, `px.line_mapbox`, `px.choropleth_map`, `px.choropleth_mapbox`, `px.density_map`, or `px.density_mapbox` function or otherwise contains one or more traces of type `go.Scattermap`, `go.Scattermapbox`,`go.Choroplethmap`, `go.Choroplethmapbox`, `go.Densitymap`, or `go.Densitymapbox` the `layout.map` or `layout.mapbox` object in your figure contains configuration information for the map itself.
4746

48-
#### How Layers Work in Mapbox Tile Maps
47+
- **Outline-based maps**
4948

50-
Mapbox tile maps are composed of various layers, of three different types:
49+
Geo maps are outline-based maps. If your figure is created with a `px.scatter_geo`, `px.line_geo` or `px.choropleth` function or otherwise contains one or more traces of type `go.Scattergeo` or `go.Choropleth`, the `layout.geo` object in your figure contains configuration information for the map itself.
5150

52-
1. `layout.mapbox.style` defines is the lowest layers, also known as your "base map"
53-
2. The various traces in `data` are by default rendered above the base map (although this can be controlled via the `below` attribute).
54-
3. `layout.mapbox.layers` is an array that defines more layers that are by default rendered above the traces in `data` (although this can also be controlled via the `below` attribute).
51+
> This page documents tile-based maps, and the [Geo map documentation](/python/map-configuration/) describes how to configure outline-based maps.
5552
56-
#### Mapbox Access Tokens and When You Need Them
53+
## Tile Map Renderers
5754

58-
The word "mapbox" in the trace names and `layout.mapbox` refers to the Mapbox GL JS open-source library, which is integrated into Plotly.py.
55+
Tile-based traces in Plotly use Maplibre or Mapbox.
5956

60-
If your basemap in `layout.mapbox.style` uses data from the Mapbox _service_, then you will need to register for a free account at https://mapbox.com/ and obtain a Mapbox Access token. This token should be provided in `layout.mapbox.access_token` (or, if using Plotly Express, via the `px.set_mapbox_access_token()` configuration function).
57+
Maplibre-based traces (new in 5.24) are ones generated in Plotly Express using `px.scatter_map`, `px.line_map`, `px.choropleth_map`, `px.density_map`, or Graph Objects using `go.Scattermap`, `go.Choroplethmap`, or `go.Densitymap`.
6158

62-
If you basemap in `layout.mapbox.style` uses maps from the [Stadia Maps service](https://www.stadiamaps.com) (see below for details), you'll need to register for a Stadia Maps account and token.
59+
Mapbox-based traces are suffixed with `mapbox`, for example `go.Scattermapbox`. These are deprecated as of version 5.24 and we recommend using the Maplibre-based traces.
6360

61+
### Maplibre
6462

65-
#### Base Maps in `layout.mapbox.style`
63+
*New in 5.24*
6664

67-
The accepted values for `layout.mapbox.style` are one of:
65+
Maplibre-based tile maps have three different types of layers:
6866

69-
- `"white-bg"` yields an empty white canvas which results in no external HTTP requests
70-
- `"open-street-map"`, `"carto-positron"`, and `"carto-darkmatter"` yield maps composed of _raster_ tiles from various public tile servers which do not require signups or access tokens.
71-
- `"basic"`, `"streets"`, `"outdoors"`, `"light"`, `"dark"`, `"satellite"`, or `"satellite-streets"` yield maps composed of _vector_ tiles from the Mapbox service, and _do_ require a Mapbox Access Token or an on-premise Mapbox installation.
72-
- `"stamen-terrain"`, `"stamen-toner"` or `"stamen-watercolor"` yield maps composed of _raster_ tiles from the [Stadia Maps service](https://www.stadiamaps.com), and require a Stadia Maps account and token.
73-
- A Mapbox service style URL, which requires a Mapbox Access Token or an on-premise Mapbox installation.
74-
- A Mapbox Style object as defined at https://docs.mapbox.com/mapbox-gl-js/style-spec/
67+
- `layout.map.style` defines the lowest layers of the map, also known as the "base map".
68+
- The various traces in `data` are by default rendered above the base map (although this can be controlled via the `below` attribute).
69+
- `layout.map.layers` is an array that defines more layers that are by default rendered above the traces in `data` (although this can also be controlled via the `below` attribute.
7570

76-
#### OpenStreetMap tiles: no token needed
7771

78-
Here is a simple map rendered with OpenStreetMaps tiles, without needing a Mapbox Access Token:
72+
#### Base Maps in `layout.map.style`.
73+
74+
The accepted values for `layout.map.style` are one of:
75+
76+
- "basic"
77+
- "carto-darkmatter"
78+
- "carto-darkmatter-nolabels"
79+
- "carto-positron"
80+
- "carto-positron-nolabels"
81+
- "carto-voyager"
82+
- "carto-voyager-nolabels"
83+
- "dark"
84+
- "light"
85+
- "open-street-map"
86+
- "outdoors"
87+
- "satellite"
88+
- "satellite-streets"
89+
- "streets"
90+
- "white-bg" - an empty white canvas which results in no external HTTP requests
91+
92+
- A custom style URL. For example: https://tiles.stadiamaps.com/styles/stamen_watercolor.json?api_key=YOUR-API-KEY
93+
94+
- A Map Style object as defined at https://maplibre.org/maplibre-style-spec/
7995

96+
97+
#### OpenStreetMap tiles
98+
99+
Here is a simple map rendered with OpenStreetMaps tiles.
80100
<!-- #endregion -->
81101

82102
```python
@@ -85,36 +105,34 @@ us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/maste
85105

86106
import plotly.express as px
87107

88-
fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
108+
fig = px.scatter_map(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
89109
color_discrete_sequence=["fuchsia"], zoom=3, height=300)
90-
fig.update_layout(mapbox_style="open-street-map")
110+
fig.update_layout(map_style="open-street-map")
91111
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
92112
fig.show()
93113
```
94114

115+
#### Using `layout.map.layers` to Specify a Base Map
95116

96-
#### Using `layout.mapbox.layers` to Specify a Base Map
97-
98-
If you have access to your own private tile servers, or wish to use a tile server not included in the list above, the recommended approach is to set `layout.mapbox.style` to `"white-bg"` and to use `layout.mapbox.layers` with `below` to specify a custom base map.
117+
If you have access to your own private tile servers, or wish to use a tile server not included in the list above, the recommended approach is to set `layout.map.style` to `"white-bg"` and to use `layout.map.layers` with `below` to specify a custom base map.
99118

100119
> If you omit the `below` attribute when using this approach, your data will likely be hidden by fully-opaque raster tiles!
101120
102121
#### Base Tiles from the USGS: no token needed
103122

104-
Here is an example of a map which uses a public USGS imagery map, specified in `layout.mapbox.layers`, and which is rendered _below_ the `data` layer.
105-
123+
Here is an example of a map which uses a public USGS imagery map, specified in `layout.map.layers`, and which is rendered _below_ the `data` layer.
106124

107125
```python
108126
import pandas as pd
109127
us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")
110128

111129
import plotly.express as px
112130

113-
fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
131+
fig = px.scatter_map(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
114132
color_discrete_sequence=["fuchsia"], zoom=3, height=300)
115133
fig.update_layout(
116-
mapbox_style="white-bg",
117-
mapbox_layers=[
134+
map_style="white-bg",
135+
map_layers=[
118136
{
119137
"below": 'traces',
120138
"sourcetype": "raster",
@@ -129,9 +147,9 @@ fig.show()
129147
```
130148

131149

132-
#### Base Tiles from the USGS, radar overlay from Environment Canada: no token needed
150+
#### Base Tiles from the USGS, radar overlay from Environment Canada
133151

134-
Here is the same example, with in addition, a WMS layer from Environment Canada which displays near-real-time radar imagery in partly-transparent raster tiles, rendered above the `go.Scattermapbox` trace, as is the default:
152+
Here is the same example, with in addition, a WMS layer from Environment Canada which displays near-real-time radar imagery in partly-transparent raster tiles, rendered above the `go.Scattermap` trace, as is the default:
135153

136154

137155
```python
@@ -140,11 +158,11 @@ us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/maste
140158

141159
import plotly.express as px
142160

143-
fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
161+
fig = px.scatter_map(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
144162
color_discrete_sequence=["fuchsia"], zoom=3, height=300)
145163
fig.update_layout(
146-
mapbox_style="white-bg",
147-
mapbox_layers=[
164+
map_style="white-bg",
165+
map_layers=[
148166
{
149167
"below": 'traces',
150168
"sourcetype": "raster",
@@ -166,32 +184,127 @@ fig.show()
166184
```
167185

168186

169-
#### Dark tiles from Mapbox service: free token needed
187+
#### Dark tiles example
170188

171-
Here is a map rendered with the `"dark"` style from the Mapbox service, which requires an Access Token:
189+
Here is a map rendered with the `"dark"` style.
190+
191+
```python
192+
import pandas as pd
193+
us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")
172194

195+
import plotly.express as px
196+
197+
fig = px.scatter_map(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
198+
color_discrete_sequence=["fuchsia"], zoom=3, height=300)
199+
fig.update_layout(map_style="dark")
200+
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
201+
fig.show()
202+
```
203+
204+
<!-- #region -->
205+
### Mapbox
206+
207+
#### How Layers Work in Mapbox Tile Maps
208+
209+
Mapbox tile maps are composed of various layers, of three different types:
210+
211+
1. `layout.mapbox.style` defines is the lowest layers, also known as your "base map"
212+
2. The various traces in `data` are by default rendered above the base map (although this can be controlled via the `below` attribute).
213+
3. `layout.mapbox.layers` is an array that defines more layers that are by default rendered above the traces in `data` (although this can also be controlled via the `below` attribute).
214+
215+
#### Mapbox Access Tokens and When You Need Them
216+
217+
The word "mapbox" in the trace names and `layout.mapbox` refers to the Mapbox GL JS open-source library, which is integrated into Plotly.py.
218+
219+
If your basemap in `layout.mapbox.style` uses data from the Mapbox _service_, then you will need to register for a free account at https://mapbox.com/ and obtain a Mapbox Access token. This token should be provided in `layout.mapbox.access_token` (or, if using Plotly Express, via the `px.set_mapbox_access_token()` configuration function).
220+
221+
If you basemap in `layout.mapbox.style` uses maps from the [Stadia Maps service](https://www.stadiamaps.com) (see below for details), you'll need to register for a Stadia Maps account and token.
222+
223+
224+
#### Base Maps in `layout.mapbox.style`
225+
226+
The accepted values for `layout.mapbox.style` are one of:
227+
228+
- `"white-bg"` yields an empty white canvas which results in no external HTTP requests
229+
- `"open-street-map"`, `"carto-positron"`, and `"carto-darkmatter"` yield maps composed of _raster_ tiles from various public tile servers which do not require signups or access tokens.
230+
- `"basic"`, `"streets"`, `"outdoors"`, `"light"`, `"dark"`, `"satellite"`, or `"satellite-streets"` yield maps composed of _vector_ tiles from the Mapbox service, and _do_ require a Mapbox Access Token or an on-premise Mapbox installation.
231+
- `"stamen-terrain"`, `"stamen-toner"` or `"stamen-watercolor"` yield maps composed of _raster_ tiles from the [Stadia Maps service](https://www.stadiamaps.com), and require a Stadia Maps account and token.
232+
- A Mapbox service style URL, which requires a Mapbox Access Token or an on-premise Mapbox installation.
233+
- A Mapbox Style object as defined at https://docs.mapbox.com/mapbox-gl-js/style-spec/
234+
235+
#### OpenStreetMap tiles: no token needed
236+
237+
Here is a simple map rendered with OpenStreetMaps tiles, without needing a Mapbox Access Token:
238+
<!-- #endregion -->
173239

174240
```python
175-
token = open(".mapbox_token").read() # you will need your own token
241+
import pandas as pd
242+
us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")
243+
244+
import plotly.express as px
245+
246+
fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
247+
color_discrete_sequence=["fuchsia"], zoom=3, height=300)
248+
fig.update_layout(mapbox_style="open-street-map")
249+
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
250+
fig.show()
251+
```
252+
253+
#### Using `layout.mapbox.layers` to Specify a Base Map
254+
255+
If you have access to your own private tile servers, or wish to use a tile server not included in the list above, the recommended approach is to set `layout.mapbox.style` to `"white-bg"` and to use `layout.mapbox.layers` with `below` to specify a custom base map.
256+
257+
> If you omit the `below` attribute when using this approach, your data will likely be hidden by fully-opaque raster tiles!
258+
259+
#### Base Tiles from the USGS: no token needed
176260

261+
Here is an example of a map which uses a public USGS imagery map, specified in `layout.mapbox.layers`, and which is rendered _below_ the `data` layer.
262+
263+
264+
```python
177265
import pandas as pd
178266
us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")
179267

180268
import plotly.express as px
181269

182270
fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
183271
color_discrete_sequence=["fuchsia"], zoom=3, height=300)
184-
fig.update_layout(mapbox_style="dark", mapbox_accesstoken=token)
272+
fig.update_layout(
273+
mapbox_style="white-bg",
274+
mapbox_layers=[
275+
{
276+
"below": 'traces',
277+
"sourcetype": "raster",
278+
"sourceattribution": "United States Geological Survey",
279+
"source": [
280+
"https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"
281+
]
282+
}
283+
])
185284
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
186285
fig.show()
187286
```
188287

189-
#### Using a mapbox image layer to display a datashader raster image
288+
#### Dark tiles from Mapbox service: free token needed
190289

191-
See the example in the [plotly and datashader tutorial](/python/datashader).
290+
Here is a map rendered with the `"dark"` style from the Mapbox service, which requires an Access Token:
192291

292+
```python
293+
token = open(".mapbox_token").read() # you will need your own token
193294

194-
#### Setting Map Bounds
295+
import pandas as pd
296+
us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")
297+
298+
import plotly.express as px
299+
300+
fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
301+
color_discrete_sequence=["fuchsia"], zoom=3, height=300)
302+
fig.update_layout(mapbox_style="dark", mapbox_accesstoken=token)
303+
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
304+
fig.show()
305+
```
306+
307+
## Setting Map Bounds
195308

196309
*New in 5.11*
197310

@@ -205,7 +318,7 @@ us_cities = pd.read_csv(
205318
"https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv"
206319
)
207320

208-
fig = px.scatter_mapbox(
321+
fig = px.scatter_map(
209322
us_cities,
210323
lat="lat",
211324
lon="lon",
@@ -215,12 +328,12 @@ fig = px.scatter_mapbox(
215328
zoom=3,
216329
height=300,
217330
)
218-
fig.update_layout(mapbox_style="open-street-map")
331+
fig.update_layout(map_style="open-street-map")
219332
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
220-
fig.update_layout(mapbox_bounds={"west": -180, "east": -50, "south": 20, "north": 90})
333+
fig.update_layout(map_bounds={"west": -180, "east": -50, "south": 20, "north": 90})
221334
fig.show()
222335
```
223336

224337
#### Reference
225338

226-
See https://plotly.com/python/reference/layout/mapbox/ for more information and options!
339+
See https://plotly.com/python/reference/layout/map/ for more information and options on Maplibre-based tile maps and https://plotly.com/python/reference/layout/mapbox/ for Mapbox-based tile maps.

0 commit comments

Comments
 (0)