Skip to content

Commit da99a7a

Browse files
geopandas choropleth examples
1 parent 7dcfbdd commit da99a7a

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

Diff for: binder/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ cufflinks==0.17.3
1818
kaleido
1919
scikit-learn
2020
umap-learn
21+
wget

Diff for: doc/python/choropleth-maps.md

+25-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.2'
9-
jupytext_version: 1.3.1
9+
jupytext_version: 1.4.2
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.7
2424
plotly:
2525
description: How to make choropleth maps in Python with Plotly.
2626
display_as: maps
@@ -164,7 +164,29 @@ fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
164164
fig.show()
165165
```
166166

167-
<!-- #region -->
167+
### Using GeoPandas Data Frames
168+
169+
`px.choropleth` accepts the `geometry` of a GeoPandas data frame as the input to `geojson`.
170+
171+
```python
172+
import plotly.express as px
173+
import geopandas as gpd
174+
import wget
175+
176+
# download and load a zipped shapefile into a GeoDataFrame
177+
wget.download("https://plotly.github.io/datasets/nyc_boroughs.zip")
178+
geo_df = gpd.read_file("zip://nyc_boroughs.zip")
179+
180+
181+
fig = px.choropleth(geo_df,
182+
geojson=geo_df.geometry,
183+
locations=geo_df.index,
184+
color='shape_leng',
185+
hover_name="boro_name")
186+
fig.update_geos(fitbounds="locations", visible=False)
187+
fig.show()
188+
```
189+
168190

169191
### Using Built-in Country and State Geometries
170192

@@ -179,7 +201,6 @@ Plotly comes with two built-in geometries which do not require an external GeoJS
179201
180202
To use the built-in countries geometry, provide `locations` as [three-letter ISO country codes](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3).
181203

182-
<!-- #endregion -->
183204

184205
```python
185206
import plotly.express as px

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

+27-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.2'
9-
jupytext_version: 1.3.1
9+
jupytext_version: 1.4.2
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.7
2424
plotly:
2525
description: How to make a Mapbox Choropleth Map of US Counties in Python with
2626
Plotly.
@@ -139,6 +139,30 @@ fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
139139
fig.show()
140140
```
141141

142+
### Using GeoPandas Data Frames
143+
144+
`px.choropleth_mapbox` accepts the `geometry` of a GeoPandas data frame as the input to `geojson`.
145+
146+
```python
147+
import plotly.express as px
148+
import geopandas as gpd
149+
import wget
150+
151+
# download and load a zipped shapefile into a GeoDataFrame
152+
wget.download("https://plotly.github.io/datasets/nyc_boroughs.zip")
153+
geo_df = gpd.read_file("zip://nyc_boroughs.zip")
154+
155+
fig = px.choropleth_mapbox(geo_df,
156+
geojson=geo_df.geometry,
157+
locations=geo_df.index,
158+
color='shape_leng',
159+
hover_name="boro_name",
160+
center={"lat": 40.71, "lon": -74.00},
161+
mapbox_style="open-street-map",
162+
zoom=8)
163+
fig.show()
164+
```
165+
142166
### Discrete Colors
143167

144168
In addition to [continuous colors](/python/colorscales/), we can [discretely-color](/python/discrete-color/) our choropleth maps by setting `color` to a non-numerical column, like the name of the winner of an election.
@@ -209,4 +233,4 @@ fig.show()
209233

210234
#### Reference
211235

212-
See https://plotly.com/python/reference/choroplethmapbox/ for more information about mapbox and their attribute options.
236+
See https://plotly.com/python/reference/choroplethmapbox/ for more information about mapbox and their attribute options.

Diff for: doc/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ pyarrow
2828
cufflinks==0.17.3
2929
kaleido
3030
umap-learn
31+
wget

0 commit comments

Comments
 (0)