Skip to content
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
30 changes: 30 additions & 0 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -100,6 +101,7 @@ class Figure:
def __init__(self) -> None:
self._name = unique_name()
self._preview_dir = TemporaryDirectory(prefix=f"{self._name}-preview-")
self._current_region: np.ndarray | None = None # Track the current region
self._activate_figure()

def __del__(self) -> None:
Expand All @@ -124,11 +126,39 @@ 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: str | Sequence[float] | np.ndarray | None
) -> 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,
Expand Down
3 changes: 3 additions & 0 deletions pygmt/src/basemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
3 changes: 3 additions & 0 deletions pygmt/src/coast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading