@@ -498,6 +498,9 @@ def __init__(self, fig, rect,
498498 self .set_label (label )
499499 self .set_figure (fig )
500500
501+ self ._invertedx = False
502+ self ._invertedy = False
503+
501504 # this call may differ for non-sep axes, eg polar
502505 self ._init_axis ()
503506
@@ -1422,10 +1425,25 @@ def set_axis_bgcolor(self, color):
14221425
14231426 ### data limits, ticks, tick labels, and formatting
14241427
1428+ def invert_xaxis (self , invert = True ):
1429+ "Invert the x-axis if 'invert' is True."
1430+ self ._invertedx = invert
1431+
1432+ def xaxis_inverted (self ):
1433+ 'Returns True if the x-axis is inverted.'
1434+ return self ._invertedx
1435+
14251436 def get_xlim (self ):
1426- 'Get the x axis range [xmin, xmax]'
1427- return self .viewLim .intervalx
1437+ """Get the x-axis range [xmin, xmax]
14281438
1439+ NOTE: The returned values are always [xmin, xmax] such that
1440+ xmin < xmax; regardless of whether or not the axes are inverted.
1441+ """
1442+ bound1 , bound2 = self .viewLim .intervalx
1443+ if ( self ._invertedx ):
1444+ return bound2 , bound1
1445+ else :
1446+ return bound1 , bound2
14291447
14301448 def set_xlim (self , xmin = None , xmax = None , emit = True , ** kwargs ):
14311449 """
@@ -1463,9 +1481,21 @@ def set_xlim(self, xmin=None, xmax=None, emit=True, **kwargs):
14631481 if xmin is None : xmin = old_xmin
14641482 if xmax is None : xmax = old_xmax
14651483
1466- xmin , xmax = mtransforms .nonsingular (xmin , xmax , increasing = False )
1484+ # provided for backwards compatability
1485+ if ( xmax < xmin ):
1486+ # swap the values so that xmin < xmax and set inverted flag
1487+ tmp = xmin
1488+ xmin = xmax
1489+ xmax = tmp
1490+ self .invert_xaxis ( True )
1491+
1492+ if ( self ._invertedx ):
1493+ xmax , xmin = mtransforms .nonsingular (xmax , xmin , increasing = False )
1494+ self .viewLim .intervalx = (xmax , xmin )
1495+ else :
1496+ xmin , xmax = mtransforms .nonsingular (xmin , xmax , increasing = False )
1497+ self .viewLim .intervalx = (xmin , xmax )
14671498
1468- self .viewLim .intervalx = (xmin , xmax )
14691499 if emit :
14701500 self .callbacks .process ('xlim_changed' , self )
14711501 # Call all of the other x-axes that are shared with this one
@@ -1534,9 +1564,25 @@ def set_xticklabels(self, labels, fontdict=None, **kwargs):
15341564 return self .xaxis .set_ticklabels (labels , fontdict , ** kwargs )
15351565 set_xticklabels .__doc__ = cbook .dedent (set_xticklabels .__doc__ ) % martist .kwdocd
15361566
1567+ def invert_yaxis (self , invert = True ):
1568+ "Invert the y-axis if 'invert' is True."
1569+ self ._invertedy = invert
1570+
1571+ def yaxis_inverted (self ):
1572+ 'Returns True if the y-axis is inverted.'
1573+ return self ._invertedy
1574+
15371575 def get_ylim (self ):
1538- 'Get the y axis range [ymin, ymax]'
1539- return self .viewLim .intervaly
1576+ """Get the y-axis range [xmin, xmax]
1577+
1578+ NOTE: The returned values are always [ymin, ymax] such that
1579+ ymin < ymax; regardless of whether or not the axes are inverted.
1580+ """
1581+ bound1 , bound2 = self .viewLim .intervaly
1582+ if ( self ._invertedy ):
1583+ return bound2 , bound1
1584+ else :
1585+ return bound1 , bound2
15401586
15411587 def set_ylim (self , ymin = None , ymax = None , emit = True , ** kwargs ):
15421588 """
@@ -1572,8 +1618,21 @@ def set_ylim(self, ymin=None, ymax=None, emit=True, **kwargs):
15721618 if ymin is None : ymin = old_ymin
15731619 if ymax is None : ymax = old_ymax
15741620
1575- ymin , ymax = mtransforms .nonsingular (ymin , ymax , increasing = False )
1576- self .viewLim .intervaly = (ymin , ymax )
1621+ # provided for backwards compatability
1622+ if ( ymax < ymin ):
1623+ # swap the values so that ymin < ymax and set inverted flag
1624+ tmp = ymin
1625+ ymin = ymax
1626+ ymax = tmp
1627+ self .invert_yaxis ( True )
1628+
1629+ if ( self ._invertedy ):
1630+ ymax , ymin = mtransforms .nonsingular (ymax , ymin , increasing = False )
1631+ self .viewLim .intervaly = (ymax , ymin )
1632+ else :
1633+ ymin , ymax = mtransforms .nonsingular (ymin , ymax , increasing = False )
1634+ self .viewLim .intervaly = (ymin , ymax )
1635+
15771636 if emit :
15781637 self .callbacks .process ('ylim_changed' , self )
15791638 # Call all of the other y-axes that are shared with this one
@@ -1582,7 +1641,6 @@ def set_ylim(self, ymin=None, ymax=None, emit=True, **kwargs):
15821641 other .set_ylim (self .viewLim .ymin , self .viewLim .ymax , emit = False )
15831642
15841643 self .figure .canvas .draw_idle ()
1585-
15861644 return ymin , ymax
15871645
15881646 def get_yscale (self ):
0 commit comments