diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index cd03957dbbfa..9eb083321e41 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -5409,7 +5409,12 @@ def get_interp_point(idx): np.column_stack([ind[where], dep2[where]])]) if ind_dir == "y": pts = pts[:, ::-1] - self.update_datalim(pts, updatex=True, updatey=True) + + up_x = up_y = True + if "transform" in kwargs: + up_x, up_y = kwargs["transform"].contains_branch_seperately(self.transData) + self.update_datalim(pts, updatex=up_x, updatey=up_y) + self.add_collection(collection, autolim=False) self._request_autoscale_view() return collection diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 863e3e2f86a6..1d0457d17855 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -8558,3 +8558,19 @@ def test_ecdf_invalid(): plt.ecdf([1, np.nan]) with pytest.raises(ValueError): plt.ecdf(np.ma.array([1, 2], mask=[True, False])) + + +def test_fill_between_axes_limits(): + fig, ax = plt.subplots() + x = np.arange(0, 4 * np.pi, 0.01) + y = 0.1*np.sin(x) + threshold = 0.075 + ax.plot(x, y, color='black') + + original_lims = (ax.get_xlim(), ax.get_ylim()) + + ax.axhline(threshold, color='green', lw=2, alpha=0.7) + ax.fill_between(x, 0, 1, where=y > threshold, + color='green', alpha=0.5, transform=ax.get_xaxis_transform()) + + assert (ax.get_xlim(), ax.get_ylim()) == original_lims