8000 Merge pull request #13330 from anntzer/getsetinverted · matplotlib/matplotlib@0135bb6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0135bb6

Browse files
authored
Merge pull request #13330 from anntzer/getsetinverted
Add Axis.get_inverted and Axis.set_inverted.
2 parents 615046f + 01aedbc commit 0135bb6

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Added `Axis.get_inverted` and `Axis.set_inverted`
2+
`````````````````````````````````````````````````
3+
4+
The `Axis.get_inverted` and `Axis.set_inverted` methods query and set whether
5+
the axis uses "inverted" orientation (i.e. increasing to the left for the
6+
x-axis and to the bottom for the y-axis).
7+
8+
They perform tasks similar to `Axes.xaxis_inverted`, `Axes.yaxis_inverted`,
9+
`Axes.invert_xaxis`, and `Axes.invert_yaxis`, with the specific difference that
10+
`.set_inverted` makes it easier to set the invertedness of an axis regardless
11+
of whether it had previously been inverted before.

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2998,7 +2998,7 @@ def invert_xaxis(self):
29982998
get_xlim, set_xlim
29992999
get_xbound, set_xbound
30003000
"""
3001-
self.set_xlim(self.get_xlim()[::-1], auto=None)
3001+
self.xaxis.set_inverted(not self.xaxis.get_inverted())
30023002

30033003
def xaxis_inverted(self):
30043004
"""
@@ -3012,8 +3012,7 @@ def xaxis_inverted(self):
30123012
get_xlim, set_xlim
30133013
get_xbound, set_xbound
30143014
"""
3015-
left, right = self.get_xlim()
3016-
return right < left
3015+
return self.xaxis.get_inverted()
30173016

30183017
def get_xbound(self):
30193018
"""
@@ -3404,7 +3403,7 @@ def invert_yaxis(self):
34043403
get_ylim, set_ylim
34053404
get_ybound, set_ybound
34063405
"""
3407-
self.set_ylim(self.get_ylim()[::-1], auto=None)
3406+
self.yaxis.set_inverted(not self.yaxis.get_inverted())
34083407

34093408
def yaxis_inverted(self):
34103409
"""
@@ -3418,8 +3417,7 @@ def yaxis_inverted(self):
34183417
get_ylim, set_ylim
34193418
get_ybound, set_ybound
34203419
"""
3421-
bottom, top = self.get_ylim()
3422-
return top < bottom
3420+
return self.yaxis.get_inverted()
34233421

34243422
def get_ybound(self):
34253423
"""

lib/matplotlib/axis.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,31 @@ def set_data_interval(self, vmin, vmax, ignore=False):
968968
"""
969969
raise NotImplementedError('Derived must override')
970970

971+
def get_inverted(self):
972+
"""
973+
Return whether the axis is oriented in the "inverse" direction.
974+
975+
The "normal" direction is increasing to the right for the x-axis and to
976+
the top for the y-axis; the "inverse" direction is increasing to the
977+
left for the x-axis and to the bottom for the y-axis.
978+
"""
979+
low, high = self.get_view_interval()
980+
return high < low
981+
982+
def set_inverted(self, inverted):
983+
"""
984+
Set whether the axis is oriented in the "inverse" direction.
985+
986+
The "normal" direction is increasing to the right for the x-axis and to
987+
the top for the y-axis; the "inverse" direction is increasing to the
988+
left for the x-axis and to the bottom for the y-axis.
989+
"""
990+
a, b = self.get_view_interval()
991+
if inverted:
992+
self.set_view_interval(max(a, b), min(a, b), ignore=True)
993+
else:
994+
self.set_view_interval(min(a, b), max(a, b), ignore=True)
995+
971996
def set_default_intervals(self):
972997
"""
973998
Set the default limits for the axis data and view interval if they

0 commit comments

Comments
 (0)
0