Skip to content

Commit

Permalink
Add Figure.choropleth to plot choropleth maps
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Oct 30, 2024
1 parent 8eb2b4f commit e5f21af
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
37 changes: 14 additions & 23 deletions examples/gallery/maps/choropleth_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
Choropleth map
==============
The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such
as polygons which are stored in a :class:`geopandas.GeoDataFrame` object. Use
:func:`geopandas.read_file` to load data from any supported OGR format such as
a shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. You can also
use a full URL pointing to your desired data source. Then, pass the
:class:`geopandas.GeoDataFrame` as an argument to the ``data`` parameter of
:meth:`pygmt.Figure.plot`, and style the geometry using the ``pen`` parameter.
To fill the polygons based on a corresponding column you need to set
``fill="+z"`` as well as select the appropriate column using the ``aspatial``
parameter as shown in the example below.
The :meth:`pygmt.Figure.choropleth` method allows us to plot geographical data such as
polygons which are stored in a :class:`geopandas.GeoDataFrame` object or a OGR_GMT file.
Use :func:`geopandas.read_file` to load data from any supported OGR formats such as a
shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. You can also use a full
URL pointing to your desired data source. Then, pass the :class:`geopandas.GeoDataFrame`
as an argument to the ``data`` parameter of :meth:`pygmt.Figure.choropleth`, and style
the geometry using the ``pen`` parameter. To fill the polygons based on a corresponding
column you need to specify the colum name to the ``column`` parameter.
"""

# %%
Expand All @@ -29,27 +27,20 @@
frame="+tPopulation of Chicago",
)

# The dataset contains different attributes, here we select
# the "population" column to plot.
# 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.
# 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,
)

# 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",
fill="+z",
cmap=True,
aspatial="Z=population",
)
# Next, we plot the polygons and fill them using the defined colormap. The target column
# is specified by the `column` parameter.
fig.choropleth(data=gdf, column="population", pen="0.3p,gray10", cmap=True)

# Add colorbar legend
fig.colorbar(frame="x+lPopulation", position="jML+o-0.5c+w3.5c/0.2c")
Expand Down
1 change: 1 addition & 0 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ def _repr_html_(self):

from pygmt.src import ( # type: ignore [misc]
basemap,
choropleth,
coast,
colorbar,
contour,
Expand Down
1 change: 1 addition & 0 deletions pygmt/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pygmt.src.basemap import basemap
from pygmt.src.binstats import binstats
from pygmt.src.blockm import blockmean, blockmedian, blockmode
from pygmt.src.choropleth import choropleth
from pygmt.src.coast import coast
from pygmt.src.colorbar import colorbar
from pygmt.src.config import config
Expand Down
29 changes: 29 additions & 0 deletions pygmt/src/choropleth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
choropleth - Plot a choropleth map.
"""

import contextlib

with contextlib.suppress(ImportError):
import geopandas as gpd


def choropleth(self, data: gpd.GeoDataFrame, column: str, **kwargs):
"""
Plot a choropleth map.
Parameters
----------
data
A :class:`geopandas.DataFrame` object or a OGR_GMT file containing the geometry
and data to plot.
column
The name of the data column to use for the fill.
"""
self.plot(

Check warning on line 23 in pygmt/src/choropleth.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/choropleth.py#L23

Added line #L23 was not covered by tests
data=data,
close=True,
fill="+z",
aspatial=f"Z={column}",
**kwargs,
)

0 comments on commit e5f21af

Please sign in to comment.