Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b7d1d59

Browse files
committed
Merged from trunk (a somewhat hairy manual merge this time). Fixed
bug (on this branch only) where inverted axes were broken. svn path=/branches/transforms/; revision=3915
1 parent b3c6c00 commit b7d1d59

6 files changed

Lines changed: 121 additions & 27 deletions

File tree

lib/matplotlib/axes.py

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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):

lib/matplotlib/backends/backend_qt.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,35 @@ def keyReleaseEvent( self, event ):
136136
def resizeEvent( self, event ):
137137
if DEBUG: print 'resize (%d x %d)' % (event.size().width(), event.size().height())
138138
qt.QWidget.resizeEvent( self, event )
139+
w = event.size().width()
140+
h = event.size().height()
141+
if DEBUG: print "FigureCanvasQt.resizeEvent(", w, ",", h, ")"
142+
dpival = self.figure.dpi.get()
143+
winch = w/dpival
144+
hinch = h/dpival
145+
self.figure.set_size_inches( winch, hinch )
146+
self.draw()
139147

140148
def resize( self, w, h ):
149+
# Pass through to Qt to resize the widget.
141150
qt.QWidget.resize( self, w, h )
142151

152+
# Resize the figure by converting pixels to inches.
153+
pixelPerInch = self.figure.dpi.get()
154+
wInch = w / pixelPerInch
155+
hInch = h / pixelPerInch
156+
self.figure.set_size_inches( wInch, hInch )
157+
158+
# Redraw everything.
159+
self.draw()
160+
161+
def sizeHint( self ):
162+
w, h = self.get_width_height()
163+
return qt.QSize( w, h )
164+
165+
def minumumSizeHint( self ):
166+
return qt.QSize( 10, 10 )
167+
143168
def _get_key( self, event ):
144169
if event.key() < 256:
145170
key = event.text().latin1()

lib/matplotlib/backends/backend_qt4.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,35 @@ def keyReleaseEvent( self, event ):
135135
def resizeEvent( self, event ):
136136
if DEBUG: print 'resize (%d x %d)' % (event.size().width(), event.size().height())
137137
QtGui.QWidget.resizeEvent( self, event )
138+
w = event.size().width()
139+
h = event.size().height()
140+
if DEBUG: print "FigureCanvasQtAgg.resizeEvent(", w, ",", h, ")"
141+
dpival = self.figure.dpi.get()
142+
winch = w/dpival
143+
hinch = h/dpival
144+
self.figure.set_size_inches( winch, hinch )
145+
self.draw()
138146

139147
def resize( self, w, h ):
148+
# Pass through to Qt to resize the widget.
140149
QtGui.QWidget.resize( self, w, h )
141150

151+
# Resize the figure by converting pixels to inches.
152+
pixelPerInch = self.figure.dpi.get()
153+
wInch = w / pixelPerInch
154+
hInch = h / pixelPerInch
155+
self.figure.set_size_inches( wInch, hInch )
156+
157+
# Redraw everything.
158+
self.draw()
159+
160+
def sizeHint( self ):
161+
w, h = self.get_width_height()
162+
return QtCore.QSize( w, h )
163+
164+
def minumumSizeHint( self ):
165+
return QtCore.QSize( 10, 10 )
166+
142167
def _get_key( self, event ):
143168
if event.key() < 256:
144169
key = str(event.text())

lib/matplotlib/backends/backend_qt4agg.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,6 @@ def __init__( self, figure ):
6565

6666
def resizeEvent( self, e ):
6767
FigureCanvasQT.resizeEvent( self, e )
68-
w = e.size().width()
69-
h = e.size().height()
70-
if DEBUG: print "FigureCanvasQtAgg.resizeEvent(", w, ",", h, ")"
71-
dpival = self.figure.dpi.get()
72-
winch = w/dpival
73-
hinch = h/dpival
74-
self.figure.set_size_inches( winch, hinch )
75-
self.draw()
7668

7769
def drawRectangle( self, rect ):
7870
self.rect = rect

lib/matplotlib/backends/backend_qtagg.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,6 @@ def __init__( self, figure ):
6464

6565
def resizeEvent( self, e ):
6666
FigureCanvasQT.resizeEvent( self, e )
67-
w = e.size().width()
68-
h = e.size().height()
69-
if DEBUG: print "FigureCanvasQtAgg.resizeEvent(", w, ",", h, ")"
70-
dpival = self.figure.dpi.get()
71-
winch = w/dpival
72-
hinch = h/dpival
73-
self.figure.set_size_inches( winch, hinch )
74-
self.draw()
7567

7668
def drawRectangle( self, rect ):
7769
self.rect = rect

lib/matplotlib/transforms.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,10 +1951,12 @@ def nonsingular(vmin, vmax, expander=0.001, tiny=1e-15, increasing=True):
19511951

19521952
# MGDTODO: Optimize (perhaps in an extension)
19531953
def interval_contains(interval, val):
1954-
return interval[0] <= val and interval[1] >= val
1954+
return ((interval[0] <= val and interval[1] >= val) or
1955+
(interval[1] <= val and interval[0] >= val))
19551956

19561957
def interval_contains_open(interval, val):
1957-
return interval[0] < val and interval[1] > val
1958+
return ((interval[0] < val and interval[1] > val) or
1959+
(interval[1] < val and interval[0] > val))
19581960

19591961
if __name__ == '__main__':
19601962
import copy

0 commit comments

Comments
 (0)