From 0e95296d33ed29abb8cfdab7ad19a7aef1f61284 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 21 Jun 2019 14:44:41 +0200 Subject: [PATCH] Fix inversion of shared axes. set_view_interval does not update shared axes, we must use set_xlim/set_ylim to do so. Update a test to explicitly invert an axis instead of relying on imshow to implicitly do so. --- lib/matplotlib/axis.py | 19 ++++++++++++++----- lib/matplotlib/tests/test_axes.py | 12 +++++++----- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index e6293a8db830..9dc8fd6d4275 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1028,11 +1028,10 @@ def set_inverted(self, inverted): the top for the y-axis; the "inverse" direction is increasing to the left for the x-axis and to the bottom for the y-axis. """ - a, b = self.get_view_interval() - if inverted: - self.set_view_interval(max(a, b), min(a, b), ignore=True) - else: - self.set_view_interval(min(a, b), max(a, b), ignore=True) + # Currently, must be implemented in subclasses using set_xlim/set_ylim + # rather than generically using set_view_interval, so that shared + # axes get updated as well. + raise NotImplementedError('Derived must override') def set_default_intervals(self): """ @@ -2171,6 +2170,11 @@ def get_ticks_position(self): def get_minpos(self): return self.axes.dataLim.minposx + def set_inverted(self, inverted): + # docstring inherited + a, b = self.get_view_interval() + self.axes.set_xlim(sorted((a, b), reverse=inverted), auto=None) + def set_default_intervals(self): # docstring inherited xmin, xmax = 0., 1. @@ -2473,6 +2477,11 @@ def get_ticks_position(self): def get_minpos(self): return self.axes.dataLim.minposy + def set_inverted(self, inverted): + # docstring inherited + a, b = self.get_view_interval() + self.axes.set_ylim(sorted((a, b), reverse=inverted), auto=None) + def set_default_intervals(self): # docstring inherited ymin, ymax = 0., 1. diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 0f39aaf5fdc4..6b41ed97be04 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -240,17 +240,19 @@ def test_inverted_cla(): ax.cla() ax.imshow(img) plt.autoscale() - assert not(ax.xaxis_inverted()) + 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 + # 5. two shared axes. Inverting the master axis should invert the 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) + ax0.yaxis.set_inverted(True) + assert ax1.yaxis_inverted() ax1.plot(x, np.cos(x)) ax0.cla() - assert not(ax1.yaxis_inverted()) + assert not ax1.yaxis_inverted() ax1.cla() # 6. clearing the nonmaster should not touch limits ax0.imshow(img)