Skip to content

Commit 9abe078

Browse files
committed
add stamen-terrain updates
1 parent d8fce58 commit 9abe078

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

doc/python/mapbox-density-heatmaps.md

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.2'
9-
jupytext_version: 1.3.0
8+
format_version: '1.3'
9+
jupytext_version: 1.15.1
1010
kernelspec:
11-
display_name: Python 3
11+
display_name: Python 3 (ipykernel)
1212
language: python
1313
name: python3
1414
language_info:
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.7.3
23+
version: 3.10.4
2424
plotly:
2525
description: How to make a Mapbox Density Heatmap in Python with Plotly.
2626
display_as: maps
@@ -33,42 +33,60 @@ jupyter:
3333
thumbnail: thumbnail/mapbox-density.png
3434
---
3535

36-
#### Mapbox Access Token
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-
40-
### Stamen Terrain base map (no token needed): density mapbox with `plotly.express`
36+
### Stamen Terrain base map (Stadia Maps token needed): density mapbox with `plotly.express`
4137

4238
[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/).
4339

4440
With `px.density_mapbox`, each row of the DataFrame is represented as a point smoothed with a given radius of influence.
4541

42+
To use the mapbox_style "stamen-terrain", you'll need a [Stadia Maps](https://www.stadiamaps.com) token, which you can provide to the `mapbox_accesstoken` parameter on `fig.update_layout`.
43+
44+
Here, we have the token saved in a file called `.mapbox_token`, load it in to the variable `token`, and then pass it to `mapbox_accesstoken`.
45+
46+
<!-- #region -->
4647
```python
48+
import plotly.express as px
4749
import pandas as pd
50+
51+
token = open(".mapbox_token").read() # you will need your own token
52+
4853
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')
4954

50-
import plotly.express as px
5155
fig = px.density_mapbox(df, lat='Latitude', lon='Longitude', z='Magnitude', radius=10,
5256
center=dict(lat=0, lon=180), zoom=0,
5357
mapbox_style="stamen-terrain")
58+
fig.update_layout(mapbox_accesstoken=token)
5459
fig.show()
5560
```
61+
<!-- #endregion -->
62+
63+
![Stamen terrain Plotly Express example](https://raw.githubusercontent.com/plotly/documentation/gh-pages/all_static/images/stamen-terrain-1.jpeg)
5664

57-
### Stamen Terrain base map (no token needed): density mapbox with `plotly.graph_objects`
65+
66+
### Stamen Terrain base map (Stadia Maps token needed): density mapbox with `plotly.graph_objects`
5867

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

70+
<!-- #region -->
6171
```python
72+
import plotly.graph_objects as go
6273
import pandas as pd
74+
75+
token = open(".mapbox_token").read() # you will need your own token
76+
6377
quakes = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')
6478

65-
import plotly.graph_objects as go
6679
fig = go.Figure(go.Densitymapbox(lat=quakes.Latitude, lon=quakes.Longitude, z=quakes.Magnitude,
6780
radius=10))
68-
fig.update_layout(mapbox_style="stamen-terrain", mapbox_center_lon=180)
81+
fig.update_layout(mapbox_style="stamen-terrain", mapbox_center_lon=180, mapbox_accesstoken=token)
6982
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
7083
fig.show()
84+
7185
```
86+
<!-- #endregion -->
87+
88+
![Stamen terrain Graph Objects example](https://raw.githubusercontent.com/plotly/documentation/gh-pages/all_static/images/stamen-terrain-2.jpeg)
89+
7290

7391
#### Reference
7492

0 commit comments

Comments
 (0)