Skip to content

Commit 9ecae0a

Browse files
mapbox examples with px (#1998)
* mapbox examples with px * Update doc/python/mapbox-density-heatmaps.md Co-Authored-By: Nicolas Kruchten <[email protected]>
1 parent 61fb14b commit 9ecae0a

File tree

2 files changed

+65
-12
lines changed

2 files changed

+65
-12
lines changed

Diff for: doc/python/mapbox-county-choropleth.md

+39-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.1'
9-
jupytext_version: 1.1.1
8+
format_version: '1.2'
9+
jupytext_version: 1.3.0
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.8
23+
version: 3.7.3
2424
plotly:
2525
description: How to make a Mapbox Choropleth Map of US Counties in Python with
2626
Plotly.
@@ -40,7 +40,9 @@ jupyter:
4040
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.
4141

4242

43-
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.
43+
### Introduction: main parameters for choropleth mapbox charts
44+
45+
Making choropleth maps 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` (`color` for `px.choropleth_mapbox`) attribute, in the same order as the IDs are passed into the `location` attribute.
4446

4547

4648
#### GeoJSON with `feature.id`
@@ -67,7 +69,39 @@ df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-
6769
df.head()
6870
```
6971

70-
#### Carto base map: no token needed
72+
### Choropleth map using plotly.express and carto base map (no token needed)
73+
74+
[Plotly Express](/python/plotly-express/) is the easy-to-use, high-level interface to Plotly, which [operates on "tidy" data](/python/px-arguments/).
75+
76+
With `px.choropleth_mapbox`, each row of the DataFrame is represented as a region of the choropleth.
77+
78+
```python
79+
from urllib.request import urlopen
80+
import json
81+
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
82+
counties = json.load(response)
83+
84+
import pandas as pd
85+
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
86+
dtype={"fips": str})
87+
88+
import plotly.express as px
89+
90+
fig = px.choropleth_mapbox(df, geojson=counties, locations='fips', color='unemp',
91+
color_continuous_scale="Viridis",
92+
range_color=(0, 12),
93+
mapbox_style="carto-positron",
94+
zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
95+
opacity=0.5,
96+
labels={'unemp':'unemployment rate'}
97+
)
98+
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
99+
fig.show()
100+
```
101+
102+
### Choropleth map using plotly.graph_objects and carto base map (no token needed)
103+
104+
If Plotly Express does not provide a good starting point, it is also possible to use the more generic `go.Choroplethmapbox` function from `plotly.graph_objects`.
71105

72106
```python
73107
from urllib.request import urlopen

Diff for: doc/python/mapbox-density-heatmaps.md

+26-7
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.1'
9-
jupytext_version: 1.1.1
8+
format_version: '1.2'
9+
jupytext_version: 1.3.0
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,10 +20,9 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.7
23+
version: 3.7.3
2424
plotly:
25-
description: How to make a Mapbox Density Heatmap in Python
26-
with Plotly.
25+
description: How to make a Mapbox Density Heatmap in Python with Plotly.
2726
display_as: maps
2827
language: python
2928
layout: base
@@ -42,14 +41,34 @@ To plot on Mapbox maps with Plotly you *may* need a Mapbox account and a public
4241

4342

4443

45-
#### Stamen Terrain base map: no token needed
44+
### Stamen Terrain base map (no token needed): density mapbox with `plotly.express`
45+
46+
[Plotly Express](/python/plotly-express/) is the easy-to-use, high-level interface to Plotly, which [operates on "tidy" data](/python/px-arguments/).
47+
48+
With `px.density_mapbox`, each row of the DataFrame is represented as a point smoothed with a given radius of influence.
49+
50+
```python
51+
import pandas as pd
52+
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')
53+
54+
import plotly.express as px
55+
fig = px.density_mapbox(df, lat='Latitude', lon='Longitude', z='Magnitude', radius=10,
56+
center=dict(lat=0, lon=180), zoom=0,
57+
mapbox_style="stamen-terrain")
58+
fig.show()
59+
```
60+
61+
### Stamen Terrain base map (no token needed): density mapbox with `plotly.graph_objects`
62+
63+
If Plotly Express does not provide a good starting point, it is also possible to use the more generic `go.Densitymapbox` function from `plotly.graph_objects`.
4664

4765
```python
4866
import pandas as pd
4967
quakes = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')
5068

5169
import plotly.graph_objects as go
52-
fig = go.Figure(go.Densitymapbox(lat=quakes.Latitude, lon=quakes.Longitude, z=quakes.Magnitude, radius=10))
70+
fig = go.Figure(go.Densitymapbox(lat=quakes.Latitude, lon=quakes.Longitude, z=quakes.Magnitude,
71+
radius=10))
5372
fig.update_layout(mapbox_style="stamen-terrain", mapbox_center_lon=180)
5473
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
5574
fig.show()

0 commit comments

Comments
 (0)