diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index bf44d1176cc7..5cb4f5443173 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -997,6 +997,10 @@ def cla(self): self.xaxis.set_minor_locator(minl) else: self.xaxis._set_scale('linear') + try: + self.set_xlim(0, 1) + except TypeError: + pass if self._sharey is not None: self.yaxis.major = self._sharey.yaxis.major @@ -1020,6 +1024,10 @@ def cla(self): self.yaxis.set_minor_locator(minl) else: self.yaxis._set_scale('linear') + try: + self.set_ylim(0, 1) + except TypeError: + pass # update the minor locator for x and y axis based on rcParams if (rcParams['xtick.minor.visible']): @@ -1110,6 +1118,7 @@ def cla(self): if self._sharey: self.yaxis.set_visible(yaxis_visible) self.patch.set_visible(patch_visible) + self.stale = True @cbook.deprecated("2.1", alternative="Axes.patch") diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 39dea8698af4..49b6a57e67ac 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -178,6 +178,54 @@ def test_twin_inherit_autoscale_setting(): assert not ax_y_off.get_autoscaley_on() +def test_inverted_cla(): + # Github PR #5450. Setting autoscale should reset + # axes to be non-inverted. + # plotting an image, then 1d graph, axis is now down + fig = plt.figure(0) + ax = fig.gca() + # 1. test that a new axis is not inverted per default + assert not(ax.xaxis_inverted()) + assert not(ax.yaxis_inverted()) + img = np.random.random((100, 100)) + ax.imshow(img) + # 2. test that a image axis is inverted + assert not(ax.xaxis_inverted()) + assert ax.yaxis_inverted() + # 3. test that clearing and plotting a line, axes are + # not inverted + ax.cla() + x = np.linspace(0, 2*np.pi, 100) + ax.plot(x, np.cos(x)) + assert not(ax.xaxis_inverted()) + assert not(ax.yaxis_inverted()) + + # 4. autoscaling should not bring back axes to normal + ax.cla() + ax.imshow(img) + plt.autoscale() + assert not(ax.xaxis_inverted()) + assert ax.yaxis_inverted() + + # 5. two shared axes. Clearing the master axis should bring axes in shared + # axes back to normal + ax0 = plt.subplot(211) + ax1 = plt.subplot(212, sharey=ax0) + ax0.imshow(img) + ax1.plot(x, np.cos(x)) + ax0.cla() + assert not(ax1.yaxis_inverted()) + ax1.cla() + # 6. clearing the nonmaster should not touch limits + ax0.imshow(img) + ax1.plot(x, np.cos(x)) + ax1.cla() + assert ax.yaxis_inverted() + + # clean up + plt.close(fig) + + @image_comparison(baseline_images=["minorticks_on_rcParams_both"], extensions=['png']) def test_minorticks_on_rcParams_both(): diff --git a/lib/mpl_toolkits/mplot3d/axes3d.py b/lib/mpl_toolkits/mplot3d/axes3d.py index 2b44de576dc0..f7156e07f015 100644 --- a/lib/mpl_toolkits/mplot3d/axes3d.py +++ b/lib/mpl_toolkits/mplot3d/axes3d.py @@ -1076,6 +1076,7 @@ def cla(self): # Disabling mouse interaction might have been needed a long # time ago, but I can't find a reason for it now - BVR (2012-03) #self.disable_mouse_rotation() + Axes.cla(self) self.zaxis.cla() if self._sharez is not None: @@ -1086,11 +1087,14 @@ def cla(self): self.zaxis._set_scale(self._sharez.zaxis.get_scale()) else: self.zaxis._set_scale('linear') + try: + self.set_zlim(0, 1) + except TypeError: + pass self._autoscaleZon = True self._zmargin = 0 - Axes.cla(self) self.grid(rcParams['axes3d.grid']) diff --git a/lib/mpl_toolkits/tests/test_mplot3d.py b/lib/mpl_toolkits/tests/test_mplot3d.py index 7ba561c6d866..10296866e76a 100644 --- a/lib/mpl_toolkits/tests/test_mplot3d.py +++ b/lib/mpl_toolkits/tests/test_mplot3d.py @@ -564,3 +564,23 @@ def test_invalid_axes_limits(setter, side, value): obj = fig.add_subplot(111, projection='3d') with pytest.raises(ValueError): getattr(obj, setter)(**limit) + + +def test_inverted_cla(): + # Github PR #5450. Setting autoscale should reset + # axes to be non-inverted. + fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) + # 1. test that a new axis is not inverted per default + assert not ax.xaxis_inverted() + assert not ax.yaxis_inverted() + assert not ax.zaxis_inverted() + ax.set_xlim(1, 0) + ax.set_ylim(1, 0) + ax.set_zlim(1, 0) + assert ax.xaxis_inverted() + assert ax.yaxis_inverted() + assert ax.zaxis_inverted() + ax.cla() + assert not ax.xaxis_inverted() + assert not ax.yaxis_inverted() + assert not ax.zaxis_inverted()