From 529d37ccacdf985a2ae9adb08c56a78a218d354d Mon Sep 17 00:00:00 2001 From: Archil Jain Date: Tue, 10 Feb 2026 14:07:46 +0530 Subject: [PATCH 1/3] Fix relim() to update limits for scatter PathCollection and add regression test --- lib/matplotlib/axes/_base.py | 5 +++++ lib/matplotlib/tests/test_axes.py | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index f89c231815dc..64faf463a014 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -29,6 +29,7 @@ import matplotlib.text as mtext import matplotlib.ticker as mticker import matplotlib.transforms as mtransforms +import matplotlib.collections as mcollections _log = logging.getLogger(__name__) @@ -2605,6 +2606,10 @@ def relim(self, visible_only=False): self._update_patch_limits(artist) elif isinstance(artist, mimage.AxesImage): self._update_image_limits(artist) + elif isinstance(artist, mcollections.Collection): + offsets = artist.get_offsets() + if offsets is not None and len(offsets): + self.update_datalim(offsets) def update_datalim(self, xys, updatex=True, updatey=True): """ diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 46843841fe93..59050e59d3be 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -10159,3 +10159,26 @@ def test_animated_artists_not_drawn_by_default(): mocked_im_draw.assert_not_called() mocked_ln_draw.assert_not_called() + +def test_relim_updates_scatter_offsets(): + import numpy as np + import matplotlib.pyplot as plt + + fig, ax = plt.subplots() + + xs = np.linspace(0, 10, 100) + ys = np.sin(xs) + scatter = ax.scatter(xs, ys) + + # Shift scatter upward + new_ys = np.sin(xs) + 5 + scatter.set_offsets(np.column_stack((xs, new_ys))) + + ax.relim() + ax.autoscale_view() + + ymin, ymax = ax.get_ylim() + + # New limits should reflect shifted data + assert ymin > 3 + assert ymax > 5 From defd13eaa16f88b59192dd20aa762270022c3c0d Mon Sep 17 00:00:00 2001 From: Archil Jain Date: Tue, 10 Feb 2026 16:45:48 +0530 Subject: [PATCH 2/3] Refactor: use _update_collection_limits for scatter handling in relim --- lib/matplotlib/axes/_base.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 64faf463a014..9e515a7a8aa0 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2416,6 +2416,12 @@ def _update_image_limits(self, image): xmin, xmax, ymin, ymax = image.get_extent() self.axes.update_datalim(((xmin, ymin), (xmax, ymax))) + def _update_collection_limits(self, collection): + offsets = collection.get_offsets() + if offsets is not None and len(offsets): + self.update_datalim(offsets) + + def add_line(self, line): """ Add a `.Line2D` to the Axes; return the line. @@ -2607,9 +2613,7 @@ def relim(self, visible_only=False): elif isinstance(artist, mimage.AxesImage): self._update_image_limits(artist) elif isinstance(artist, mcollections.Collection): - offsets = artist.get_offsets() - if offsets is not None and len(offsets): - self.update_datalim(offsets) + self._update_collection_limits(artist) def update_datalim(self, xys, updatex=True, updatey=True): """ From fbb623e7f8cf25595a4c8bc1e852f4a7d22f39ab Mon Sep 17 00:00:00 2001 From: Archil Jain Date: Wed, 11 Feb 2026 00:37:34 +0530 Subject: [PATCH 3/3] Fix ruff E302: add missing blank line before test --- lib/matplotlib/tests/test_axes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 59050e59d3be..6e6614090311 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -10160,6 +10160,7 @@ def test_animated_artists_not_drawn_by_default(): mocked_im_draw.assert_not_called() mocked_ln_draw.assert_not_called() + def test_relim_updates_scatter_offsets(): import numpy as np import matplotlib.pyplot as plt