Repository: matplotlib/matplotlib

Issue:
[Bug]: Incorrect X-axis scaling with date values
### Bug summary

In matplotlib 3.9.0, when plotting dates on the X-axis, calling axhspan unexpectedly expands the X-axis range from 1970 to 2024. This behavior did not occur in version 3.8.4, where the X-axis correctly ranges from 2020 to 2024.

### Code for reproduction

```Python
import datetime
import matplotlib.pyplot as plt

x = [datetime.date(year, 1, 1) for year in range(2020, 2024)]
y = range(4)

plt.plot(x, y)
plt.axhspan(1, 2)
```


### Actual outcome

![3 9 0](https://github.com/matplotlib/matplotlib/assets/6249977/104927cc-75f7-4a3d-abc7-d66d84b16d93)


### Expected outcome

![3 8 4](https://github.com/matplotlib/matplotlib/assets/6249977/3b628921-5cc3-4b9b-b2ce-1570d82d0bb4)


### Additional information

_No response_

### Operating system

Fedora

### Matplotlib Version

3.9.0

### Matplotlib Backend

inline or Agg

### Python version

3.12.3

### Jupyter version

4.2.1

### Installation

pip


Discussion:
This is a regression in 3.9.
Most likely related to https://github.com/matplotlib/matplotlib/pull/26788 
A bisect does indeed point to that commit; I'm a bit confused though as it seems like that change is supposed to try and avoid changing the datalimits, but maybe it's something to do with using date/units.

cc @anntzer
Woops.  The fix is easy:
```patch
diff --git i/lib/matplotlib/axes/_axes.py w/lib/matplotlib/axes/_axes.py
index 9a2b367fb5..7efbd6562f 100644
--- i/lib/matplotlib/axes/_axes.py
+++ w/lib/matplotlib/axes/_axes.py
@@ -1028,7 +1028,7 @@ class Axes(_AxesBase):
         # 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
```
(intervalx is an array and thus vulnerable to getting mutated in place).  Note that axvspan already has the necessary copy() call, only axhspan was missing it.
Can you take over the patch?



Patch:
diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py
index 328dda4a6a71..5e69bcb57d7f 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


Test patch:
diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py
index 48121ee04939..13c181b68492 100644
--- a/lib/matplotlib/tests/test_axes.py
+++ b/lib/matplotlib/tests/test_axes.py
@@ -8250,10 +8250,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():