From d135fcc434f40672520244429b4c28ff5cccf7b4 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 25 Jun 2026 19:50:12 +0800 Subject: [PATCH 1/2] Refactor choropleth implementation --- pygmt/src/choropleth.py | 75 ++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 13 deletions(-) diff --git a/pygmt/src/choropleth.py b/pygmt/src/choropleth.py index edc561e0f32..8a5e1bab4ae 100644 --- a/pygmt/src/choropleth.py +++ b/pygmt/src/choropleth.py @@ -2,27 +2,43 @@ choropleth - Plot a choropleth map. """ +from collections.abc import Sequence +from typing import Literal + from pygmt._typing import GeoLike, PathLike +from pygmt.alias import Alias, AliasSystem +from pygmt.clib import Session +from pygmt.helpers import build_arg_list, fmt_docstring +from pygmt.params import Axis, Frame __doctest_skip__ = ["choropleth"] -def choropleth( +@fmt_docstring +def choropleth( # noqa: PLR0913 self, data: GeoLike | PathLike, column: str, cmap: str | bool = True, - **kwargs, + intensity: float | None = None, + pen: str | None = None, + no_clip: bool = False, + projection: str | None = None, + region: Sequence[float | str] | str | None = None, + frame: Frame | Axis | Literal["none"] | str | Sequence[str] | bool = False, + verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"] + | bool = False, + panel: int | Sequence[int] | bool = False, + perspective: float | Sequence[float] | str | bool = False, + transparency: float | None = None, ): """ Plot a choropleth map. - This method is a thin wrapper around :meth:`pygmt.Figure.plot` that sets the - appropriate parameters for creating a choropleth map by filling polygons based on - values in a specified data column. It requires the input data to be a geo-like - Python object that implements ``__geo_interface__`` (e.g. a - :class:`geopandas.GeoDataFrame`), or an OGR_GMT file containing the geometry and - data to plot. + This method fills polygons based on values in a specified data column. It requires + the input data to be a geo-like Python object that implements ``__geo_interface__`` + (e.g. a :class:`geopandas.GeoDataFrame`), or an OGR_GMT file containing the + geometry and data to plot. Parameters ---------- @@ -35,8 +51,19 @@ def choropleth( cmap The CPT to use for filling the polygons. If set to ``True``, the current CPT will be used. - **kwargs - Additional keyword arguments passed to :meth:`pygmt.Figure.plot`. + intensity + The intensity (nominally in the ±1 range) to modulate the fill color by + simulating illumination [Default is no illumination]. + no_clip + Do **not** clip polygons that fall outside the frame boundaries. + $pen + $projection + $region + $frame + $verbose + $panel + $perspective + $transparency Examples -------- @@ -46,7 +73,7 @@ def choropleth( ... "https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip" ... ) >>> world["POP_EST"] *= 1e-6 # Population in millions - + >>> >>> fig = pygmt.Figure() >>> fig.basemap(region=[-19.5, 53, -38, 37.5], projection="M15c", frame=True) >>> pygmt.makecpt(cmap="bilbao", series=(0, 270, 10), reverse=True) @@ -54,6 +81,28 @@ def choropleth( >>> fig.colorbar(frame=True) >>> fig.show() """ - self.plot( - data=data, close=True, fill="+z", cmap=cmap, aspatial=f"Z={column}", **kwargs + self._activate_figure() + + aliasdict = AliasSystem( + C=Alias(cmap, name="cmap"), + I=Alias(intensity, name="intensity"), + N=Alias(no_clip, name="no_clip"), + W=Alias(pen, name="pen"), + a=Alias(f"Z={column}", name="column"), + ).add_common( + B=frame, + J=projection, + R=region, + V=verbose, + c=panel, + p=perspective, + t=transparency, ) + # Force -G+z and -L to be set for choropleth + aliasdict.update({"G": "+z", "L": True}) + + with Session() as lib: + with lib.virtualfile_in(check_kind="vector", data=data) as vintbl: + lib.call_module( + module="plot", args=build_arg_list(aliasdict, infile=vintbl) + ) From 3db6abdfa8e3b95f38b68dbf72377604d1b78ff5 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 25 Jun 2026 20:11:00 +0800 Subject: [PATCH 2/2] Add aliases --- pygmt/src/choropleth.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pygmt/src/choropleth.py b/pygmt/src/choropleth.py index 8a5e1bab4ae..7af400db00c 100644 --- a/pygmt/src/choropleth.py +++ b/pygmt/src/choropleth.py @@ -40,6 +40,24 @@ def choropleth( # noqa: PLR0913 (e.g. a :class:`geopandas.GeoDataFrame`), or an OGR_GMT file containing the geometry and data to plot. + **Aliases:** + + .. hlist:: + :columns: 3 + + - B = frame + - C = cmap + - I = intensity + - J = projection + - R = region + - N = no_clip + - W = pen + - V = verbose + - a = column + - c = panel + - p = perspective + - t = transparency + Parameters ---------- data