From 3935ba9bde8deae127f4b8d9440a540732e88ca5 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Mon, 29 Jun 2020 12:55:37 -0700 Subject: [PATCH] Backport PR #17781: Fix limit setting after plotting empty data --- lib/matplotlib/axes/_axes.py | 4 +--- lib/matplotlib/axes/_base.py | 2 +- lib/matplotlib/tests/test_axes.py | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index eaf94407e1ea..251cc32f362a 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -5291,9 +5291,7 @@ def get_interp_point(idx): np.column_stack([ind[where], dep2[where]])]) if ind_dir == "y": pts = pts[:, ::-1] - self.dataLim.update_from_data_xy(pts, self.ignore_existing_data_limits, - updatex=True, updatey=True) - self.ignore_existing_data_limits = False + self.update_datalim(pts, updatex=True, updatey=True) self.add_collection(collection, autolim=False) self._request_autoscale_view() return collection diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index e83f69f54445..90e14580bfc2 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2144,7 +2144,7 @@ def update_datalim(self, xys, updatex=True, updatey=True): Whether to update the x/y limits. """ xys = np.asarray(xys) - if not len(xys): + if not np.any(np.isfinite(xys)): return self.dataLim.update_from_data_xy(xys, self.ignore_existing_data_limits, updatex=updatex, updatey=updatey) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 33a4fe2914f9..1d50b1bf2cf0 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -779,6 +779,23 @@ def test_nonfinite_limits(): ax.plot(x, y) +@pytest.mark.style('default') +@pytest.mark.parametrize('plot_fun', + ['scatter', 'plot', 'fill_between']) +@check_figures_equal(extensions=["png"]) +def test_limits_empty_data(plot_fun, fig_test, fig_ref): + # Check that plotting empty data doesn't change autoscaling of dates + x = np.arange("2010-01-01", "2011-01-01", dtype="datetime64[D]") + + ax_test = fig_test.subplots() + ax_ref = fig_ref.subplots() + + getattr(ax_test, plot_fun)([], []) + + for ax in [ax_test, ax_ref]: + getattr(ax, plot_fun)(x, range(len(x)), color='C0') + + @image_comparison(['imshow', 'imshow'], remove_text=True, style='mpl20') def test_imshow(): # use former defaults to match existing baseline image