Skip to content

Commit 602814d

Browse files
geopandas scatter examples
1 parent da99a7a commit 602814d

File tree

4 files changed

+63
-26
lines changed

4 files changed

+63
-26
lines changed

doc/python/choropleth-maps.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ fig.show()
166166

167167
### Using GeoPandas Data Frames
168168

169-
`px.choropleth` accepts the `geometry` of a GeoPandas data frame as the input to `geojson`.
169+
`px.choropleth` accepts the `geometry` of a GeoPandas data frame as the input to `geojson` if the `geometry` contains polygons.
170170

171171
```python
172172
import plotly.express as px

doc/python/mapbox-county-choropleth.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,27 @@ fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
139139
fig.show()
140140
```
141141

142+
### Discrete Colors
143+
144+
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.
145+
146+
```python
147+
import plotly.express as px
148+
149+
df = px.data.election()
150+
geojson = px.data.election_geojson()
151+
152+
fig = px.choropleth_mapbox(df, geojson=geojson, color="winner",
153+
locations="district", featureidkey="properties.district",
154+
center={"lat": 45.5517, "lon": -73.7073},
155+
mapbox_style="carto-positron", zoom=9)
156+
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
157+
fig.show()
158+
```
159+
142160
### Using GeoPandas Data Frames
143161

144-
`px.choropleth_mapbox` accepts the `geometry` of a GeoPandas data frame as the input to `geojson`.
162+
`px.choropleth_mapbox` accepts the `geometry` of a GeoPandas data frame as the input to `geojson` if the `geometry` contains polygons.
145163

146164
```python
147165
import plotly.express as px
@@ -163,24 +181,6 @@ fig = px.choropleth_mapbox(geo_df,
163181
fig.show()
164182
```
165183

166-
### Discrete Colors
167-
168-
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.
169-
170-
```python
171-
import plotly.express as px
172-
173-
df = px.data.election()
174-
geojson = px.data.election_geojson()
175-
176-
fig = px.choropleth_mapbox(df, geojson=geojson, color="winner",
177-
locations="district", featureidkey="properties.district",
178-
center={"lat": 45.5517, "lon": -73.7073},
179-
mapbox_style="carto-positron", zoom=9)
180-
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
181-
fig.show()
182-
```
183-
184184
### Choropleth map using plotly.graph_objects and carto base map (no token needed)
185185

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

doc/python/scatter-plots-on-maps.md

+21-3
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.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.7
23+
version: 3.7.7
2424
plotly:
2525
description: How to make scatter plots on maps in Python. Scatter plots on maps
2626
highlight geographic areas and can be colored by value.
@@ -66,6 +66,24 @@ fig = px.scatter_geo(df, locations="iso_alpha",
6666
fig.show()
6767
```
6868

69+
### Basic Example with GeoPandas
70+
71+
`px.scatter_geo` can work well with GeoPandas dataframes whose `geometry` is of type `Point`.
72+
73+
```python
74+
import plotly.express as px
75+
import geopandas as gpd
76+
77+
geo_df = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
78+
79+
px.set_mapbox_access_token(open(".mapbox_token").read())
80+
fig = px.scatter_geo(geo_df,
81+
lat=geo_df.geometry.y,
82+
lon=geo_df.geometry.x,
83+
hover_name="name")
84+
fig.show()
85+
```
86+
6987
### U.S. Airports Map
7088

7189
Here we show how to use `go.Scattergeo` from `plotly.graph_objects`.

doc/python/scattermapbox.md

+22-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.2
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.7.3
23+
version: 3.7.7
2424
plotly:
2525
description: How to make scatter plots on Mapbox maps in Python.
2626
display_as: maps
@@ -52,6 +52,25 @@ fig = px.scatter_mapbox(df, lat="centroid_lat", lon="centroid_lon", color="p
5252
fig.show()
5353
```
5454

55+
### Basic Example with GeoPandas
56+
57+
`px.scatter_mapbox` can work well with GeoPandas dataframes whose `geometry` is of type `Point`.
58+
59+
```python
60+
import plotly.express as px
61+
import geopandas as gpd
62+
63+
geo_df = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
64+
65+
px.set_mapbox_access_token(open(".mapbox_token").read())
66+
fig = px.scatter_mapbox(geo_df,
67+
lat=geo_df.geometry.y,
68+
lon=geo_df.geometry.x,
69+
hover_name="name",
70+
zoom=1)
71+
fig.show()
72+
```
73+
5574
#### Basic Example
5675

5776
```python
@@ -228,4 +247,4 @@ fig.show()
228247

229248
#### Reference
230249

231-
See https://plotly.com/python/reference/scattermapbox/ for more information and options!
250+
See https://plotly.com/python/reference/scattermapbox/ for more information and options!

0 commit comments

Comments
 (0)