Skip to content
39 changes: 15 additions & 24 deletions examples/gallery/maps/choropleth_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,36 @@
"""

# %%
import geodatasets
import geopandas as gpd
import pygmt

# Read the example dataset provided by geodatasets.
gdf = gpd.read_file(geodatasets.get_path("geoda airbnb"))
print(gdf.head())
provider = "https://naciscdn.org/naturalearth/"
world = gpd.read_file(f"{provider}50m/cultural/ne_50m_admin_0_countries.zip")
Comment thread
yvonnefroehlich marked this conversation as resolved.
Outdated
world["POP_EST"] *= 1e-6
Comment thread
yvonnefroehlich marked this conversation as resolved.

# %%
fig = pygmt.Figure()
fig.basemap(region=[-19.5, 53, -37.5, 38], projection="M15c", frame="+n")

fig.basemap(
region=gdf.total_bounds[[0, 2, 1, 3]],
projection="M6c",
frame="+tPopulation of Chicago",
)
# The dataset contains different attributes, here we focus on the population within
# the different countries (column "POP_EST").

# The dataset contains different attributes, here we select the "population" column to
# plot.

# First, we define the colormap to fill the polygons based on the "population" column.
pygmt.makecpt(
cmap="acton",
series=[gdf["population"].min(), gdf["population"].max(), 10],
continuous=True,
reverse=True,
)
# First, we define the colormap to fill the polygons based on the "POP_EST" column.
pygmt.makecpt(cmap="acton", series=(0, 100), reverse=True)

# Next, we plot the polygons and fill them using the defined colormap. The target column
# is defined by the aspatial parameter.
fig.plot(
data=gdf,
pen="0.3p,gray10",
data=world[world["CONTINENT"] == "Africa"],
Comment thread
seisman marked this conversation as resolved.
Outdated
pen="1p,gray50",
fill="+z",
cmap=True,
aspatial="Z=population",
aspatial="Z=POP_EST",
)

# Add colorbar legend.
fig.colorbar(frame="x+lPopulation", position="jML+o-0.5c+w3.5c/0.2c")
fig.colorbar(
frame="x10f5+lPopulation (millions)",
position="jML+o3c/-3.5c+w7.5c+ef0.3c+ml",
)

fig.show()