From a2310cbd1409e4ae21bc751f7bef80c5a9b5a1fc Mon Sep 17 00:00:00 2001 From: chuan Date: Wed, 14 Jan 2026 16:28:56 +0800 Subject: [PATCH 1/3] Track the current region at the PyGMT level, instead of relying on the GMT_Extract_Region function of GMT. --- pygmt/figure.py | 27 +++++++++++++++++++++++++++ pygmt/src/basemap.py | 3 +++ pygmt/src/coast.py | 3 +++ 3 files changed, 33 insertions(+) diff --git a/pygmt/figure.py b/pygmt/figure.py index 56ad2c3d5cf..a2d227a80f3 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -100,6 +100,7 @@ class Figure: def __init__(self) -> None: self._name = unique_name() self._preview_dir = TemporaryDirectory(prefix=f"{self._name}-preview-") + self._current_region = None # Track the current region self._activate_figure() def __del__(self) -> None: @@ -124,11 +125,37 @@ def region(self) -> np.ndarray: """ The geographic WESN bounding box for the current figure. """ + # First try to return the tracked current region + if self._current_region is not None: + return self._current_region + + # Fall back to extracting region from GMT (for backward compatibility) self._activate_figure() with Session() as lib: wesn = lib.extract_region() return wesn + def _update_current_region(self, region) -> None: + """ + Update the current region tracking. + + Parameters + ---------- + region : str, list, or np.ndarray + The region to set as current. + """ + if region is not None: + # Convert region to numpy array for consistency + if isinstance(region, str): + # For string regions like "g" or "JP", we can't convert to array + # We'll set it to None and let GMT handle it + self._current_region = None + else: + # Convert list or array to numpy array + self._current_region = np.asarray(region) + else: + self._current_region = None + def savefig( self, fname: PathLike, diff --git a/pygmt/src/basemap.py b/pygmt/src/basemap.py index 8c264f4f3e5..f5c1a5c9341 100644 --- a/pygmt/src/basemap.py +++ b/pygmt/src/basemap.py @@ -100,6 +100,9 @@ def basemap( """ self._activate_figure() + # Update the current region tracking + self._update_current_region(region) + aliasdict = AliasSystem( Jz=Alias(zscale, name="zscale"), JZ=Alias(zsize, name="zsize"), diff --git a/pygmt/src/coast.py b/pygmt/src/coast.py index 2c94a5403d4..79b8f48a7dc 100644 --- a/pygmt/src/coast.py +++ b/pygmt/src/coast.py @@ -226,6 +226,9 @@ def coast( # noqa: PLR0913 """ self._activate_figure() + # Update the current region tracking + self._update_current_region(region) + if ( kwargs.get("G", land) is None and kwargs.get("S", water) is None From 980dcfe4f0f370c16f3ab948ea13e2d13ede8f04 Mon Sep 17 00:00:00 2001 From: chuan Date: Wed, 14 Jan 2026 16:56:10 +0800 Subject: [PATCH 2/3] fix static type check --- pygmt/figure.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index a2d227a80f3..ae74bc1115c 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -4,6 +4,7 @@ import base64 import os +from collections.abc import Sequence from pathlib import Path from tempfile import TemporaryDirectory from typing import Literal, overload @@ -100,7 +101,7 @@ class Figure: def __init__(self) -> None: self._name = unique_name() self._preview_dir = TemporaryDirectory(prefix=f"{self._name}-preview-") - self._current_region = None # Track the current region + self._current_region: np.ndarray | None = None # Track the current region self._activate_figure() def __del__(self) -> None: @@ -135,7 +136,7 @@ def region(self) -> np.ndarray: wesn = lib.extract_region() return wesn - def _update_current_region(self, region) -> None: + def _update_current_region(self, region: str | Sequence[float] | np.ndarray | None) -> None: """ Update the current region tracking. From bf05ab09acdfa4443262d86edf0302d4d1754396 Mon Sep 17 00:00:00 2001 From: chuan Date: Wed, 14 Jan 2026 16:57:59 +0800 Subject: [PATCH 3/3] fix type check --- pygmt/figure.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index ae74bc1115c..5fef9cb4f08 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -136,7 +136,9 @@ def region(self) -> np.ndarray: wesn = lib.extract_region() return wesn - def _update_current_region(self, region: str | Sequence[float] | np.ndarray | None) -> None: + def _update_current_region( + self, region: str | Sequence[float] | np.ndarray | None + ) -> None: """ Update the current region tracking.