Skip to content

POC: Add Figure.choropleth to plot choropleth maps #2798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions examples/gallery/maps/choropleth_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +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
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.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.
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 Down Expand Up @@ -43,14 +42,8 @@
)

# 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",
)
# 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 @@ -421,6 +421,7 @@ def _repr_html_(self) -> str:

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,
)
Loading