Skip to content

Commit e5f21af

Browse files
committed
Add Figure.choropleth to plot choropleth maps
1 parent 8eb2b4f commit e5f21af

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed

examples/gallery/maps/choropleth_map.py

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
Choropleth map
33
==============
44
5-
The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such
6-
as polygons which are stored in a :class:`geopandas.GeoDataFrame` object. Use
7-
:func:`geopandas.read_file` to load data from any supported OGR format such as
8-
a shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. You can also
9-
use a full URL pointing to your desired data source. Then, pass the
10-
:class:`geopandas.GeoDataFrame` as an argument to the ``data`` parameter of
11-
:meth:`pygmt.Figure.plot`, and style the geometry using the ``pen`` parameter.
12-
To fill the polygons based on a corresponding column you need to set
13-
``fill="+z"`` as well as select the appropriate column using the ``aspatial``
14-
parameter as shown in the example below.
5+
The :meth:`pygmt.Figure.choropleth` method allows us to plot geographical data such as
6+
polygons which are stored in a :class:`geopandas.GeoDataFrame` object or a OGR_GMT file.
7+
Use :func:`geopandas.read_file` to load data from any supported OGR formats such as a
8+
shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. You can also use a full
9+
URL pointing to your desired data source. Then, pass the :class:`geopandas.GeoDataFrame`
10+
as an argument to the ``data`` parameter of :meth:`pygmt.Figure.choropleth`, and style
11+
the geometry using the ``pen`` parameter. To fill the polygons based on a corresponding
12+
column you need to specify the colum name to the ``column`` parameter.
1513
"""
1614

1715
# %%
@@ -29,27 +27,20 @@
2927
frame="+tPopulation of Chicago",
3028
)
3129

32-
# The dataset contains different attributes, here we select
33-
# the "population" column to plot.
30+
# The dataset contains different attributes, here we select the "population" column to
31+
# plot.
3432

35-
# First, we define the colormap to fill the polygons based on
36-
# the "population" column.
33+
# First, we define the colormap to fill the polygons based on the "population" column.
3734
pygmt.makecpt(
3835
cmap="acton",
3936
series=[gdf["population"].min(), gdf["population"].max(), 10],
4037
continuous=True,
4138
reverse=True,
4239
)
4340

44-
# Next, we plot the polygons and fill them using the defined colormap.
45-
# The target column is defined by the aspatial parameter.
46-
fig.plot(
47-
data=gdf,
48-
pen="0.3p,gray10",
49-
fill="+z",
50-
cmap=True,
51-
aspatial="Z=population",
52-
)
41+
# Next, we plot the polygons and fill them using the defined colormap. The target column
42+
# is specified by the `column` parameter.
43+
fig.choropleth(data=gdf, column="population", pen="0.3p,gray10", cmap=True)
5344

5445
# Add colorbar legend
5546
fig.colorbar(frame="x+lPopulation", position="jML+o-0.5c+w3.5c/0.2c")

pygmt/figure.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ def _repr_html_(self):
402402

403403
from pygmt.src import ( # type: ignore [misc]
404404
basemap,
405+
choropleth,
405406
coast,
406407
colorbar,
407408
contour,

pygmt/src/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pygmt.src.basemap import basemap
66
from pygmt.src.binstats import binstats
77
from pygmt.src.blockm import blockmean, blockmedian, blockmode
8+
from pygmt.src.choropleth import choropleth
89
from pygmt.src.coast import coast
910
from pygmt.src.colorbar import colorbar
1011
from pygmt.src.config import config

pygmt/src/choropleth.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
choropleth - Plot a choropleth map.
3+
"""
4+
5+
import contextlib
6+
7+
with contextlib.suppress(ImportError):
8+
import geopandas as gpd
9+
10+
11+
def choropleth(self, data: gpd.GeoDataFrame, column: str, **kwargs):
12+
"""
13+
Plot a choropleth map.
14+
15+
Parameters
16+
----------
17+
data
18+
A :class:`geopandas.DataFrame` object or a OGR_GMT file containing the geometry
19+
and data to plot.
20+
column
21+
The name of the data column to use for the fill.
22+
"""
23+
self.plot(
24+
data=data,
25+
close=True,
26+
fill="+z",
27+
aspatial=f"Z={column}",
28+
**kwargs,
29+
)

0 commit comments

Comments
 (0)