Skip to content

Commit ccf050d

Browse files
Update the example for geopandas polygon geometry with the Natural Earth dataset (#4228)
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
1 parent b4ebdf2 commit ccf050d

File tree

1 file changed

+12
-29
lines changed

1 file changed

+12
-29
lines changed

examples/gallery/maps/choropleth_map.py

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,28 @@
1414
"""
1515

1616
# %%
17-
import geodatasets
1817
import geopandas as gpd
1918
import pygmt
2019

21-
# Read the example dataset provided by geodatasets.
22-
gdf = gpd.read_file(geodatasets.get_path("geoda airbnb"))
23-
print(gdf.head())
20+
provider = "https://naciscdn.org/naturalearth"
21+
world = gpd.read_file(f"{provider}/110m/cultural/ne_110m_admin_0_countries.zip")
2422

25-
# %%
26-
fig = pygmt.Figure()
23+
# The dataset contains different attributes, here we focus on the population within
24+
# the different countries (column "POP_EST") for the continent "Africa".
25+
world["POP_EST"] *= 1e-6
26+
africa = world[world["CONTINENT"] == "Africa"].copy()
2727

28-
fig.basemap(
29-
region=gdf.total_bounds[[0, 2, 1, 3]],
30-
projection="M6c",
31-
frame="+tPopulation of Chicago",
32-
)
33-
34-
# The dataset contains different attributes, here we select the "population" column to
35-
# plot.
28+
fig = pygmt.Figure()
29+
fig.basemap(region=[-19.5, 53, -37.5, 38], projection="M10c", frame="+n")
3630

37-
# First, we define the colormap to fill the polygons based on the "population" column.
38-
pygmt.makecpt(
39-
cmap="acton",
40-
series=[gdf["population"].min(), gdf["population"].max(), 10],
41-
continuous=True,
42-
reverse=True,
43-
)
31+
# First, we define the colormap to fill the polygons based on the "POP_EST" column.
32+
pygmt.makecpt(cmap="acton", series=(0, 100), reverse=True)
4433

4534
# Next, we plot the polygons and fill them using the defined colormap. The target column
4635
# is defined by the aspatial parameter.
47-
fig.plot(
48-
data=gdf,
49-
pen="0.3p,gray10",
50-
fill="+z",
51-
cmap=True,
52-
aspatial="Z=population",
53-
)
36+
fig.plot(data=africa, pen="0.8p,gray50", fill="+z", cmap=True, aspatial="Z=POP_EST")
5437

5538
# Add colorbar legend.
56-
fig.colorbar(frame="x+lPopulation", position="jML+o-0.5c+w3.5c/0.2c")
39+
fig.colorbar(frame="x+lPopulation (millions)", position="jML+o2c/-2.5c+w5c+ef0.2c+ml")
5740

5841
fig.show()

0 commit comments

Comments
 (0)