From 475d5b70daa5f1b0e66dc89c9cba857d6930ea61 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sat, 29 Jun 2024 14:52:32 -0400 Subject: [PATCH] Backport PR #28487: Fix autoscaling with axhspan --- lib/matplotlib/axes/_axes.py | 2 +- lib/matplotlib/tests/test_axes.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index ffeecdcbd029..040c5a4ba4e9 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -1028,7 +1028,7 @@ def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs): # For Rectangles and non-separable transforms, add_patch can be buggy # and update the x limits even though it shouldn't do so for an # yaxis_transformed patch, so undo that update. - ix = self.dataLim.intervalx + ix = self.dataLim.intervalx.copy() mx = self.dataLim.minposx self.add_patch(p) self.dataLim.intervalx = ix diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index ff4d698fbb6e..3c0407ee4098 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -8195,10 +8195,10 @@ def test_relative_ticklabel_sizes(size): def test_multiplot_autoscale(): fig = plt.figure() ax1, ax2 = fig.subplots(2, 1, sharex='all') - ax1.scatter([1, 2, 3, 4], [2, 3, 2, 3]) + ax1.plot([18000, 18250, 18500, 18750], [2, 3, 2, 3]) ax2.axhspan(-5, 5) xlim = ax1.get_xlim() - assert np.allclose(xlim, [0.5, 4.5]) + assert np.allclose(xlim, [18000, 18800]) def test_sharing_does_not_link_positions():