From 767408723dc90a2729c9e8c68bec99ab0714cd4b Mon Sep 17 00:00:00 2001 From: aduan5567-netizen <293562499+aduan5567-netizen@users.noreply.github.com> Date: Sun, 20 Sep 2026 00:34:35 +0800 Subject: [PATCH 1/3] Fix s-grid rendering on Matplotlib 3.11 --- control/grid.py | 46 +++++++++++++++++++++++++++++++++++++- control/tests/grid_test.py | 18 +++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 control/tests/grid_test.py diff --git a/control/grid.py b/control/grid.py index a3e7f36e5..0d7f5298a 100644 --- a/control/grid.py +++ b/control/grid.py @@ -13,7 +13,7 @@ import mpl_toolkits.axisartist.angle_helper as angle_helper import numpy as np from matplotlib.projections import PolarAxes -from matplotlib.transforms import Affine2D +from matplotlib.transforms import Affine2D, Bbox from mpl_toolkits.axisartist import SubplotHost from mpl_toolkits.axisartist.grid_helper_curvelinear import \ GridHelperCurveLinear @@ -37,6 +37,12 @@ class ModifiedExtremeFinderCycle(angle_helper.ExtremeFinderCycle): https://matplotlib.org/_modules/mpl_toolkits/axisartist/angle_helper.html#ExtremeFinderCycle.__call__ """ def __call__(self, transform_xy, x1, y1, x2, y2): + # Matplotlib 3.11 moved this calculation to + # _find_transformed_bbox(), which is also called directly by + # GridFinder. Keep the legacy implementation for older releases. + if hasattr(super(), '_find_transformed_bbox'): + return super().__call__(transform_xy, x1, y1, x2, y2) + x, y = np.meshgrid( np.linspace(x1, x2, self.nx), np.linspace(y1, y2, self.ny)) lon, lat = transform_xy(np.ravel(x), np.ravel(y)) @@ -77,6 +83,44 @@ def __call__(self, transform_xy, x1, y1, x2, y2): return lon_min, lon_max, lat_min, lat_max + def _find_transformed_bbox(self, trans, bbox): + """Find grid extremes using the Matplotlib 3.11 interface.""" + grid = np.reshape(np.meshgrid( + np.linspace(bbox.x0, bbox.x1, self.nx), + np.linspace(bbox.y0, bbox.y1, self.ny)), (2, -1)).T + lon, lat = trans.transform(grid).T + + with np.errstate(invalid='ignore'): + if self.lon_cycle is not None: + lon0 = np.nanmin(lon) + # Changed from 180 to 360 to be able to span only + # 90-270 (left hand side) + lon -= 360. * ((lon - lon0) > 360.) + if self.lat_cycle is not None: # pragma: no cover + lat0 = np.nanmin(lat) + lat -= 360. * ((lat - lat0) > 180.) + + transformed_bbox = Bbox.null() + transformed_bbox.update_from_data_xy(np.column_stack([lon, lat])) + transformed_bbox = transformed_bbox.expanded( + 1 + 2 / self.nx, 1 + 2 / self.ny) + lon_min, lat_min, lon_max, lat_max = transformed_bbox.extents + + if self.lon_cycle: + lon_max = min(lon_max, lon_min + self.lon_cycle) + if self.lat_cycle: # pragma: no cover + lat_max = min(lat_max, lat_min + self.lat_cycle) + + if self.lon_minmax is not None: + lon_min = max(self.lon_minmax[0], lon_min) + lon_max = min(self.lon_minmax[1], lon_max) + + if self.lat_minmax is not None: + lat_min = max(self.lat_minmax[0], lat_min) + lat_max = min(self.lat_minmax[1], lat_max) + + return Bbox.from_extents(lon_min, lat_min, lon_max, lat_max) + def sgrid(subplot=(1, 1, 1), scaling=None): # From matplotlib demos: diff --git a/control/tests/grid_test.py b/control/tests/grid_test.py new file mode 100644 index 000000000..08ed50671 --- /dev/null +++ b/control/tests/grid_test.py @@ -0,0 +1,18 @@ +import numpy as np +from matplotlib.transforms import Affine2D, Bbox + +from control.grid import ModifiedExtremeFinderCycle + + +def test_modified_extreme_finder_cycle(): + finder = ModifiedExtremeFinderCycle( + 20, 20, lon_cycle=360, lat_cycle=None, + lon_minmax=(90, 270), lat_minmax=(0, np.inf)) + + limits = finder(lambda x, y: (x, y), 90, 0, 270, 1) + np.testing.assert_allclose(limits, (90, 270, 0, 1.05)) + + transformed_bbox = finder._find_transformed_bbox( + Affine2D(), Bbox.from_extents(90, 0, 270, 1)) + np.testing.assert_allclose( + transformed_bbox.extents, (90, 0, 270, 1.05)) From 329869c1fc8ea4d9351548078fdc751d33eb0d75 Mon Sep 17 00:00:00 2001 From: aduan5567-netizen <293562499+aduan5567-netizen@users.noreply.github.com> Date: Sun, 20 Sep 2026 11:47:17 +0800 Subject: [PATCH 2/3] Refactor extreme finder compatibility path --- control/grid.py | 48 +++++++++--------------------------------------- 1 file changed, 9 insertions(+), 39 deletions(-) diff --git a/control/grid.py b/control/grid.py index 0d7f5298a..636588145 100644 --- a/control/grid.py +++ b/control/grid.py @@ -43,52 +43,22 @@ def __call__(self, transform_xy, x1, y1, x2, y2): if hasattr(super(), '_find_transformed_bbox'): return super().__call__(transform_xy, x1, y1, x2, y2) - x, y = np.meshgrid( - np.linspace(x1, x2, self.nx), np.linspace(y1, y2, self.ny)) - lon, lat = transform_xy(np.ravel(x), np.ravel(y)) - - with np.errstate(invalid='ignore'): - if self.lon_cycle is not None: - lon0 = np.nanmin(lon) - # Changed from 180 to 360 to be able to span only - # 90-270 (left hand side) - lon -= 360. * ((lon - lon0) > 360.) - if self.lat_cycle is not None: # pragma: no cover - lat0 = np.nanmin(lat) - lat -= 360. * ((lat - lat0) > 180.) - - lon_min, lon_max = np.nanmin(lon), np.nanmax(lon) - lat_min, lat_max = np.nanmin(lat), np.nanmax(lat) - - lon_min, lon_max, lat_min, lat_max = \ - self._add_pad(lon_min, lon_max, lat_min, lat_max) - - # check cycle - if self.lon_cycle: - lon_max = min(lon_max, lon_min + self.lon_cycle) - if self.lat_cycle: # pragma: no cover - lat_max = min(lat_max, lat_min + self.lat_cycle) - - if self.lon_minmax is not None: - min0 = self.lon_minmax[0] - lon_min = max(min0, lon_min) - max0 = self.lon_minmax[1] - lon_max = min(max0, lon_max) - - if self.lat_minmax is not None: - min0 = self.lat_minmax[0] - lat_min = max(min0, lat_min) - max0 = self.lat_minmax[1] - lat_max = min(max0, lat_max) - + transformed_bbox = self._find_extremes( + lambda points: np.column_stack(transform_xy(*points.T)), + Bbox.from_extents(x1, y1, x2, y2)) + lon_min, lat_min, lon_max, lat_max = transformed_bbox.extents return lon_min, lon_max, lat_min, lat_max def _find_transformed_bbox(self, trans, bbox): """Find grid extremes using the Matplotlib 3.11 interface.""" + return self._find_extremes(trans.transform, bbox) + + def _find_extremes(self, transform, bbox): + """Find grid extremes using a point-array transform.""" grid = np.reshape(np.meshgrid( np.linspace(bbox.x0, bbox.x1, self.nx), np.linspace(bbox.y0, bbox.y1, self.ny)), (2, -1)).T - lon, lat = trans.transform(grid).T + lon, lat = transform(grid).T with np.errstate(invalid='ignore'): if self.lon_cycle is not None: From 2a140b146eb62048c6e3f912b493efbcc2a7e17f Mon Sep 17 00:00:00 2001 From: aduan5567-netizen <293562499+aduan5567-netizen@users.noreply.github.com> Date: Sun, 20 Sep 2026 14:40:16 +0800 Subject: [PATCH 3/3] Remove custom s-grid extreme finder --- control/grid.py | 73 ++++---------------------------------- control/tests/grid_test.py | 20 +++++------ 2 files changed, 14 insertions(+), 79 deletions(-) diff --git a/control/grid.py b/control/grid.py index 636588145..002786cdb 100644 --- a/control/grid.py +++ b/control/grid.py @@ -13,7 +13,7 @@ import mpl_toolkits.axisartist.angle_helper as angle_helper import numpy as np from matplotlib.projections import PolarAxes -from matplotlib.transforms import Affine2D, Bbox +from matplotlib.transforms import Affine2D from mpl_toolkits.axisartist import SubplotHost from mpl_toolkits.axisartist.grid_helper_curvelinear import \ GridHelperCurveLinear @@ -31,67 +31,6 @@ def __call__(self, direction, factor, values): return ret -class ModifiedExtremeFinderCycle(angle_helper.ExtremeFinderCycle): - """Changed to allow only left hand-side polar grid. - - https://matplotlib.org/_modules/mpl_toolkits/axisartist/angle_helper.html#ExtremeFinderCycle.__call__ - """ - def __call__(self, transform_xy, x1, y1, x2, y2): - # Matplotlib 3.11 moved this calculation to - # _find_transformed_bbox(), which is also called directly by - # GridFinder. Keep the legacy implementation for older releases. - if hasattr(super(), '_find_transformed_bbox'): - return super().__call__(transform_xy, x1, y1, x2, y2) - - transformed_bbox = self._find_extremes( - lambda points: np.column_stack(transform_xy(*points.T)), - Bbox.from_extents(x1, y1, x2, y2)) - lon_min, lat_min, lon_max, lat_max = transformed_bbox.extents - return lon_min, lon_max, lat_min, lat_max - - def _find_transformed_bbox(self, trans, bbox): - """Find grid extremes using the Matplotlib 3.11 interface.""" - return self._find_extremes(trans.transform, bbox) - - def _find_extremes(self, transform, bbox): - """Find grid extremes using a point-array transform.""" - grid = np.reshape(np.meshgrid( - np.linspace(bbox.x0, bbox.x1, self.nx), - np.linspace(bbox.y0, bbox.y1, self.ny)), (2, -1)).T - lon, lat = transform(grid).T - - with np.errstate(invalid='ignore'): - if self.lon_cycle is not None: - lon0 = np.nanmin(lon) - # Changed from 180 to 360 to be able to span only - # 90-270 (left hand side) - lon -= 360. * ((lon - lon0) > 360.) - if self.lat_cycle is not None: # pragma: no cover - lat0 = np.nanmin(lat) - lat -= 360. * ((lat - lat0) > 180.) - - transformed_bbox = Bbox.null() - transformed_bbox.update_from_data_xy(np.column_stack([lon, lat])) - transformed_bbox = transformed_bbox.expanded( - 1 + 2 / self.nx, 1 + 2 / self.ny) - lon_min, lat_min, lon_max, lat_max = transformed_bbox.extents - - if self.lon_cycle: - lon_max = min(lon_max, lon_min + self.lon_cycle) - if self.lat_cycle: # pragma: no cover - lat_max = min(lat_max, lat_min + self.lat_cycle) - - if self.lon_minmax is not None: - lon_min = max(self.lon_minmax[0], lon_min) - lon_max = min(self.lon_minmax[1], lon_max) - - if self.lat_minmax is not None: - lat_min = max(self.lat_minmax[0], lat_min) - lat_max = min(self.lat_minmax[1], lat_max) - - return Bbox.from_extents(lon_min, lat_min, lon_max, lat_max) - - def sgrid(subplot=(1, 1, 1), scaling=None): # From matplotlib demos: # https://matplotlib.org/gallery/axisartist/demo_curvelinear_grid.html @@ -101,14 +40,14 @@ def sgrid(subplot=(1, 1, 1), scaling=None): # system in degrees tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform() - # polar projection, which involves cycle, and also has limits in - # its coordinates, needs a special method to find the extremes - # (min, max of the coordinate within the view). + # The s-plane grid spans the fixed longitude interval 90-270 degrees. + # Disable cycle unwrapping at the 180-degree boundary and use lon_minmax + # to clip the grid to the left half-plane. # 20, 20 : number of sampling points along x, y direction sampling_points = 20 - extreme_finder = ModifiedExtremeFinderCycle( - sampling_points, sampling_points, lon_cycle=360, lat_cycle=None, + extreme_finder = angle_helper.ExtremeFinderCycle( + sampling_points, sampling_points, lon_cycle=None, lat_cycle=None, lon_minmax=(90, 270), lat_minmax=(0, np.inf),) grid_locator1 = angle_helper.LocatorDMS(15) diff --git a/control/tests/grid_test.py b/control/tests/grid_test.py index 08ed50671..442191deb 100644 --- a/control/tests/grid_test.py +++ b/control/tests/grid_test.py @@ -1,18 +1,14 @@ import numpy as np -from matplotlib.transforms import Affine2D, Bbox +import matplotlib.pyplot as plt -from control.grid import ModifiedExtremeFinderCycle +from control.grid import sgrid -def test_modified_extreme_finder_cycle(): - finder = ModifiedExtremeFinderCycle( - 20, 20, lon_cycle=360, lat_cycle=None, - lon_minmax=(90, 270), lat_minmax=(0, np.inf)) +def test_sgrid_does_not_unwrap_longitude(mplcleanup): + ax, fig = sgrid() + finder = ax.get_grid_helper().grid_finder.extreme_finder - limits = finder(lambda x, y: (x, y), 90, 0, 270, 1) + assert finder.lon_cycle is None + limits = finder(lambda x, y: (x, y), 90, 0, 270.001, 1) np.testing.assert_allclose(limits, (90, 270, 0, 1.05)) - - transformed_bbox = finder._find_transformed_bbox( - Affine2D(), Bbox.from_extents(90, 0, 270, 1)) - np.testing.assert_allclose( - transformed_bbox.extents, (90, 0, 270, 1.05)) + plt.close(fig)