Skip to content

Commit 2e0f184

Browse files
committed
Update scattermapbox.md
1 parent 4a70af6 commit 2e0f184

File tree

1 file changed

+90
-56
lines changed

1 file changed

+90
-56
lines changed

doc/python/scattermapbox.md

Lines changed: 90 additions & 56 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.16.2
9+
jupytext_version: 1.16.3
1010
kernelspec:
1111
display_name: Python 3 (ipykernel)
1212
language: python
@@ -22,48 +22,43 @@ jupyter:
2222
pygments_lexer: ipython3
2323
version: 3.10.0
2424
plotly:
25-
description: How to make scatter plots on Mapbox maps in Python.
25+
description: How to make scatter plots on tile maps in Python.
2626
display_as: maps
2727
language: python
2828
layout: base
2929
name: Scatter Plots on Mapbox
3030
order: 9
3131
page_type: u-guide
32-
permalink: python/scattermapbox/
32+
permalink: python/tile-scatter-maps/
3333
thumbnail: thumbnail/scatter-mapbox.jpg
34+
redirect_from: python/scattermapbox/
3435
---
3536

36-
#### Mapbox Access Token and Base Map Configuration
37-
38-
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-
4037
### Basic example with Plotly Express
4138

42-
Here we show the [Plotly Express](/python/plotly-express/) function `px.scatter_mapbox` for a scatter plot on a tile map.
39+
Here we show the [Plotly Express](/python/plotly-express/) function `px.scatter_map` for a scatter plot on a tile map.
4340

4441
[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/).
4542

4643
```python
4744
import plotly.express as px
48-
px.set_mapbox_access_token(open(".mapbox_token").read())
4945
df = px.data.carshare()
50-
fig = px.scatter_mapbox(df, lat="centroid_lat", lon="centroid_lon", color="peak_hour", size="car_hours",
46+
fig = px.scatter_map(df, lat="centroid_lat", lon="centroid_lon", color="peak_hour", size="car_hours",
5147
color_continuous_scale=px.colors.cyclical.IceFire, size_max=15, zoom=10)
5248
fig.show()
5349
```
5450

5551
### Basic Example with GeoPandas
5652

57-
`px.scatter_mapbox` can work well with [GeoPandas](https://geopandas.org/) dataframes whose `geometry` is of type `Point`.
53+
`px.scatter_map` can work well with [GeoPandas](https://geopandas.org/) dataframes whose `geometry` is of type `Point`.
5854

5955
```python
6056
import plotly.express as px
6157
import geopandas as gpd
6258

6359
geo_df = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
6460

65-
px.set_mapbox_access_token(open(".mapbox_token").read())
66-
fig = px.scatter_mapbox(geo_df,
61+
fig = px.scatter_map(geo_df,
6762
lat=geo_df.geometry.y,
6863
lon=geo_df.geometry.x,
6964
hover_name="name",
@@ -76,24 +71,21 @@ fig.show()
7671
```python
7772
import plotly.graph_objects as go
7873

79-
mapbox_access_token = open(".mapbox_token").read()
80-
81-
fig = go.Figure(go.Scattermapbox(
74+
fig = go.Figure(go.Scattermap(
8275
lat=['45.5017'],
8376
lon=['-73.5673'],
8477
mode='markers',
85-
marker=go.scattermapbox.Marker(
78+
marker=go.scattermap.Marker(
8679
size=14
8780
),
8881
text=['Montreal'],
8982
))
9083

9184
fig.update_layout(
9285
hovermode='closest',
93-
mapbox=dict(
94-
accesstoken=mapbox_access_token,
86+
map=dict(
9587
bearing=0,
96-
center=go.layout.mapbox.Center(
88+
center=go.layout.map.Center(
9789
lat=45,
9890
lon=-73
9991
),
@@ -110,9 +102,7 @@ fig.show()
110102
```python
111103
import plotly.graph_objects as go
112104

113-
mapbox_access_token = open(".mapbox_token").read()
114-
115-
fig = go.Figure(go.Scattermapbox(
105+
fig = go.Figure(go.Scattermap(
116106
lat=['38.91427','38.91538','38.91458',
117107
'38.92239','38.93222','38.90842',
118108
'38.91931','38.93260','38.91368',
@@ -124,7 +114,7 @@ fig = go.Figure(go.Scattermapbox(
124114
'-76.99656','-77.042438','-77.02821',
125115
'-77.01239'],
126116
mode='markers',
127-
marker=go.scattermapbox.Marker(
117+
marker=go.scattermap.Marker(
128118
size=9
129119
),
130120
text=["The coffee bar","Bistro Bohem","Black Cat",
@@ -137,8 +127,7 @@ fig = go.Figure(go.Scattermapbox(
137127
fig.update_layout(
138128
autosize=True,
139129
hovermode='closest',
140-
mapbox=dict(
141-
accesstoken=mapbox_access_token,
130+
map=dict(
142131
bearing=0,
143132
center=dict(
144133
lat=38.92,
@@ -158,20 +147,18 @@ fig.show()
158147
import plotly.graph_objects as go
159148
import pandas as pd
160149

161-
mapbox_access_token = open(".mapbox_token").read()
162-
163150
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/Nuclear%20Waste%20Sites%20on%20American%20Campuses.csv')
164151
site_lat = df.lat
165152
site_lon = df.lon
166153
locations_name = df.text
167154

168155
fig = go.Figure()
169156

170-
fig.add_trace(go.Scattermapbox(
157+
fig.add_trace(go.Scattermap(
171158
lat=site_lat,
172159
lon=site_lon,
173160
mode='markers',
174-
marker=go.scattermapbox.Marker(
161+
marker=go.scattermap.Marker(
175162
size=17,
176163
color='rgb(255, 0, 0)',
177164
opacity=0.7
@@ -180,11 +167,11 @@ fig.add_trace(go.Scattermapbox(
180167
hoverinfo='text'
181168
))
182169

183-
fig.add_trace(go.Scattermapbox(
170+
fig.add_trace(go.Scattermap(
184171
lat=site_lat,
185172
lon=site_lon,
186173
mode='markers',
187-
marker=go.scattermapbox.Marker(
174+
marker=go.scattermap.Marker(
188175
size=8,
189176
color='rgb(242, 177, 172)',
190177
opacity=0.7
@@ -197,8 +184,7 @@ fig.update_layout(
197184
autosize=True,
198185
hovermode='closest',
199186
showlegend=False,
200-
mapbox=dict(
201-
accesstoken=mapbox_access_token,
187+
map=dict(
202188
bearing=0,
203189
center=dict(
204190
lat=38,
@@ -215,7 +201,7 @@ fig.show()
215201

216202
### Set Marker Symbols
217203

218-
You can define a symbol on your map by setting [symbol](https://plotly.com/python/reference/scattermapbox/#scattermapbox-marker-symbol) attribute. This attribute only works on Mapbox-provided `style`s:
204+
You can define a symbol on your map by setting [symbol](https://plotly.com/python/reference/scattermap/#scattermap-marker-symbol) attribute.
219205

220206
- basic
221207
- streets
@@ -228,17 +214,14 @@ You can define a symbol on your map by setting [symbol](https://plotly.com/pytho
228214
```python
229215
import plotly.graph_objects as go
230216

231-
token = open(".mapbox_token").read() # you need your own token
232-
233-
fig = go.Figure(go.Scattermapbox(
217+
fig = go.Figure(go.Scattermap(
234218
mode = "markers+text+lines",
235219
lon = [-75, -80, -50], lat = [45, 20, -20],
236220
marker = {'size': 20, 'symbol': ["bus", "harbor", "airport"]},
237221
text = ["Bus", "Harbor", "airport"],textposition = "bottom right"))
238222

239223
fig.update_layout(
240-
mapbox = {
241-
'accesstoken': token,
224+
map = {
242225
'style': "outdoors", 'zoom': 0.7},
243226
showlegend = False)
244227

@@ -255,26 +238,23 @@ Display clusters of data points by setting `cluster`. Here, we enable clusters w
255238
import plotly.express as px
256239
import pandas as pd
257240

258-
px.set_mapbox_access_token(open(".mapbox_token").read())
259241
df = pd.read_csv(
260242
"https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv"
261243
)
262-
fig = px.scatter_mapbox(df, lat="lat", lon="long", size="cnt", zoom=3)
244+
fig = px.scatter_map(df, lat="lat", lon="long", size="cnt", zoom=3)
263245
fig.update_traces(cluster=dict(enabled=True))
264246
fig.show()
265247

266248
```
267249

268250
#### Font Customization
269251

270-
You can customize the font on `go.Scattermapbox` traces with `textfont`. For example, you can set the font `family`.
252+
You can customize the font on `go.Scattermap` traces with `textfont`. For example, you can set the font `family`.
271253

272254
```python
273255
import plotly.graph_objects as go
274256

275-
token = open(".mapbox_token").read() # you need your own token
276-
277-
fig = go.Figure(go.Scattermapbox(
257+
fig = go.Figure(go.Scattermap(
278258
mode = "markers+text+lines",
279259
lon = [-75, -80, -50], lat = [45, 20, -20],
280260
marker = {'size': 20, 'symbol': ["bus", "harbor", "airport"]},
@@ -283,15 +263,14 @@ fig = go.Figure(go.Scattermapbox(
283263
))
284264

285265
fig.update_layout(
286-
mapbox = {
287-
'accesstoken': token,
266+
map = {
288267
'style': "outdoors", 'zoom': 0.7},
289268
showlegend = False,)
290269

291270
fig.show()
292271
```
293272

294-
`go.Scattermapbox` supports the following values for `textfont.family`:
273+
`go.Scattermap` supports the following values for `textfont.family`:
295274

296275
'Metropolis Black Italic', 'Metropolis Black', 'Metropolis Bold Italic', 'Metropolis Bold', 'Metropolis Extra Bold Italic', 'Metropolis Extra Bold', 'Metropolis Extra Light Italic', 'Metropolis Extra Light', 'Metropolis Light Italic', 'Metropolis Light', 'Metropolis Medium Italic', 'Metropolis Medium', 'Metropolis Regular Italic', 'Metropolis Regular', 'Metropolis Semi Bold Italic', 'Metropolis Semi Bold', 'Metropolis Thin Italic', 'Metropolis Thin', 'Open Sans Bold Italic', 'Open Sans Bold', 'Open Sans Extrabold Italic', 'Open Sans Extrabold', 'Open Sans Italic', 'Open Sans Light Italic', 'Open Sans Light', 'Open Sans Regular', 'Open Sans Semibold Italic', 'Open Sans Semibold', 'Klokantech Noto Sans Bold', 'Klokantech Noto Sans CJK Bold', 'Klokantech Noto Sans CJK Regular', 'Klokantech Noto Sans Italic', and 'Klokantech Noto Sans Regular'.
297276

@@ -300,14 +279,12 @@ fig.show()
300279

301280
*New in 5.23*
302281

303-
You can specify a numeric font weight on `go.Scattermapbox` with `textfont.weight`.
282+
You can specify a numeric font weight on `go.Scattermap` with `textfont.weight`.
304283

305284
```python
306285
import plotly.graph_objects as go
307286

308-
token = open(".mapbox_token").read() # you need your own token
309-
310-
fig = go.Figure(go.Scattermapbox(
287+
fig = go.Figure(go.Scattermap(
311288
mode = "markers+text+lines",
312289
lon = [-75, -80, -50], lat = [45, 20, -20],
313290
marker = dict(size=20, symbol=["bus", "harbor", "airport"]),
@@ -316,14 +293,71 @@ fig = go.Figure(go.Scattermapbox(
316293
))
317294

318295
fig.update_layout(
319-
mapbox = dict(
320-
accesstoken=token,
296+
map = dict(
321297
style="outdoors", zoom=0.7),
322298
showlegend = False,)
323299

324300
fig.show()
325301
```
326302

303+
## Mapbox Maps
304+
305+
The earlier examples using `px.scatter_map` and `go.Scattermap` use Maplibre for rendering. These traces were introduced in Plotly.py 5.24. These trace types are now the recommended way to create scatter plots on tile-based maps. There are also traces that use Mapbox: `px.scatter_mapbox` and `go.Scattermapbox`
306+
307+
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.
308+
309+
Here's the first example rewritten to use `px.scatter_mapbox`.
310+
311+
```python
312+
import plotly.express as px
313+
px.set_mapbox_access_token(open(".mapbox_token").read())
314+
df = px.data.carshare()
315+
fig = px.scatter_mapbox(df, lat="centroid_lat", lon="centroid_lon", color="peak_hour", size="car_hours",
316+
color_continuous_scale=px.colors.cyclical.IceFire, size_max=15, zoom=10)
317+
fig.show()
318+
319+
```
320+
321+
And here's an example using Graph Objects:
322+
323+
```python
324+
import plotly.graph_objects as go
325+
326+
mapbox_access_token = open(".mapbox_token").read()
327+
328+
fig = go.Figure(go.Scattermapbox(
329+
lat=['45.5017'],
330+
lon=['-73.5673'],
331+
mode='markers',
332+
marker=go.scattermapbox.Marker(
333+
size=14
334+
),
335+
text=['Montreal'],
336+
))
337+
338+
fig.update_layout(
339+
hovermode='closest',
340+
mapbox=dict(
341+
accesstoken=mapbox_access_token,
342+
bearing=0,
343+
center=go.layout.mapbox.Center(
344+
lat=45,
345+
lon=-73
346+
),
347+
pitch=0,
348+
zoom=5
349+
)
350+
)
351+
352+
fig.show()
353+
```
354+
327355
#### Reference
328356

329-
See [function reference for `px.(scatter_mapbox)`](https://plotly.com/python-api-reference/generated/plotly.express.scatter_mapbox) or https://plotly.com/python/reference/scattermapbox/ for more information and options!
357+
See [function reference for `px.(scatter_map)`](https://plotly.com/python-api-reference/generated/plotly.express.scatter_mapbox) or https://plotly.com/python/reference/scattermap/ for more information about the attributes available.
358+
359+
For Mapbox-based tile maps, see [function reference for `px.(scatter_mapbox)`](https://plotly.com/python-api-reference/generated/plotly.express.scatter_mapbox) or https://plotly.com/python/reference/scattermapbox/.
360+
361+
```python
362+
363+
```

0 commit comments

Comments
 (0)