Skip to content

Commit ff60817

Browse files
committed
Update GeoPandas example for geopandas 1.0+
The example was using the 'world' dataset which was removed in geopandas 1.0. Updated to use the geodatasets package for v1.0+ with a fallback to the old approach for older versions. This keeps the example working across all geopandas versions. Fixes #4778
1 parent 4e953f3 commit ff60817

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,31 @@ fig.show()
7070

7171
`px.scatter_geo` can work well with [GeoPandas](https://geopandas.org/) dataframes whose `geometry` is of type `Point`.
7272

73+
**Note**: In geopandas 1.0+, the built-in datasets have been moved to the separate [geodatasets](https://geodatasets.readthedocs.io/) package.
74+
7375
```python
7476
import plotly.express as px
7577
import geopandas as gpd
7678

77-
geo_df = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
79+
# For geopandas >= 1.0, use the geodatasets package
80+
try:
81+
from geodatasets import get_path
82+
geo_df = gpd.read_file(get_path('naturalearth.land'))
83+
except ImportError:
84+
# Fallback for older geopandas versions (< 1.0)
85+
geo_df = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
86+
87+
# Filter to get a subset of points for visualization
88+
# For this example, we'll use country capitals or major cities
89+
geo_df = geo_df.head(10) # Take first 10 features
90+
91+
# Calculate centroids to get point geometry
92+
geo_df['geometry'] = geo_df.geometry.centroid
7893

79-
px.set_mapbox_access_token(open(".mapbox_token").read())
8094
fig = px.scatter_geo(geo_df,
8195
lat=geo_df.geometry.y,
8296
lon=geo_df.geometry.x,
83-
hover_name="name")
97+
hover_name="name" if "name" in geo_df.columns else None)
8498
fig.show()
8599
```
86100

0 commit comments

Comments
 (0)