diff --git a/doc/users/next_whats_new/2018-02-01-AL.rst b/doc/users/next_whats_new/2018-02-01-AL.rst new file mode 100644 index 000000000000..1acaba83037d --- /dev/null +++ b/doc/users/next_whats_new/2018-02-01-AL.rst @@ -0,0 +1,11 @@ +Added `Axis.get_inverted` and `Axis.set_inverted` +````````````````````````````````````````````````` + +The `Axis.get_inverted` and `Axis.set_inverted` methods query and set whether +the axis uses "inverted" orientation (i.e. increasing to the left for the +x-axis and to the bottom for the y-axis). + +They perform tasks similar to `Axes.xaxis_inverted`, `Axes.yaxis_inverted`, +`Axes.invert_xaxis`, and `Axes.invert_yaxis`, with the specific difference that +`.set_inverted` makes it easier to set the invertedness of an axis regardless +of whether it had previously been inverted before. diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 477526b308e4..8287983a3039 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2998,7 +2998,7 @@ def invert_xaxis(self): get_xlim, set_xlim get_xbound, set_xbound """ - self.set_xlim(self.get_xlim()[::-1], auto=None) + self.xaxis.set_inverted(not self.xaxis.get_inverted()) def xaxis_inverted(self): """ @@ -3012,8 +3012,7 @@ def xaxis_inverted(self): get_xlim, set_xlim get_xbound, set_xbound """ - left, right = self.get_xlim() - return right < left + return self.xaxis.get_inverted() def get_xbound(self): """ @@ -3404,7 +3403,7 @@ def invert_yaxis(self): get_ylim, set_ylim get_ybound, set_ybound """ - self.set_ylim(self.get_ylim()[::-1], auto=None) + self.yaxis.set_inverted(not self.yaxis.get_inverted()) def yaxis_inverted(self): """ @@ -3418,8 +3417,7 @@ def yaxis_inverted(self): get_ylim, set_ylim get_ybound, set_ybound """ - bottom, top = self.get_ylim() - return top < bottom + return self.yaxis.get_inverted() def get_ybound(self): """ diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index a2eb3e429bd1..0d36c2896bc6 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -909,6 +909,31 @@ def set_data_interval(self): '''set the axis data limits''' raise NotImplementedError('Derived must override') + def get_inverted(self): + """ + Return whether the axis is oriented in the "inverse" direction. + + The "normal" direction is increasing to the right for the x-axis and to + 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. + """ + low, high = self.get_view_interval() + return high < low + + def set_inverted(self, inverted): + """ + Set whether the axis is oriented in the "inverse" direction. + + The "normal" direction is increasing to the right for the x-axis and to + 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) + def set_default_intervals(self): '''set the default limits for the axis data and view interval if they are not mutated'''