Skip to content

Commit dd525e4

Browse files
committed
Another increment of aspect handling; still not finished, but
axes.set_position() now works again. svn path=/trunk/matplotlib/; revision=2186
1 parent 2bde570 commit dd525e4

File tree

3 files changed

+53
-43
lines changed

3 files changed

+53
-43
lines changed

lib/matplotlib/axes.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ def __init__(self, fig, rect,
341341
):
342342
Artist.__init__(self)
343343
self._position = map(makeValue, rect)
344+
self._originalPosition = rect
344345
# must be set before set_figure
345346
self._sharex = sharex
346347
self._sharey = sharey
@@ -372,7 +373,6 @@ def __init__(self, fig, rect,
372373
# aspect ratio atribute, and original position
373374
self.set_aspect('auto')
374375
self.set_aspect_adjusts('position')
375-
self._originalPosition = self.get_position()
376376

377377
if len(kwargs): setp(self, **kwargs)
378378

@@ -1403,8 +1403,8 @@ def draw(self, renderer=None, inframe=False):
14031403
raise RuntimeError('No renderer defined')
14041404
if not self.get_visible(): return
14051405
renderer.open_group('axes')
1406-
14071406
self.apply_aspect()
1407+
14081408
try: self.transData.freeze() # eval the lazy objects
14091409
except ValueError:
14101410
print >> sys.stderr, 'data freeze value error', self.get_position(), self.dataLim.get_bounds(), self.viewLim.get_bounds()
@@ -1477,7 +1477,6 @@ def draw(self, renderer=None, inframe=False):
14771477

14781478
self._cachedRenderer = renderer
14791479

1480-
14811480
def __draw_animate(self):
14821481
# ignore for now; broken
14831482
if self._lastRenderer is None:
@@ -2438,7 +2437,7 @@ def pcolormesh(self, *args, **kwargs):
24382437
coords[:, 1] = Y.astype(Float32)
24392438
#print coords
24402439

2441-
if shading == 'faceted':
2440+
if shading == 'faceted':
24422441
showedges = 1
24432442
else:
24442443
showedges = 0
@@ -2910,15 +2909,27 @@ def get_position(self):
29102909
'Return the axes rectangle left, bottom, width, height'
29112910
return [val.get() for val in self._position]
29122911

2913-
def set_position(self, pos):
2912+
def set_position(self, pos, which='both'):
29142913
"""
29152914
Set the axes position with pos = [left, bottom, width, height]
29162915
in relative 0,1 coords
29172916
2917+
There are two position variables: one which is ultimately
2918+
used, but which may be modified by apply_aspect, and a second
2919+
which is the starting point for apply_aspect.
2920+
2921+
which = 'active' to change the first;
2922+
'original' to change the second;
2923+
'both' to change both
2924+
29182925
ACCEPTS: len(4) sequence of floats
29192926
"""
2920-
for num,val in zip(pos, self._position):
2921-
val.set(num)
2927+
if which in ('both', 'active'):
2928+
# Change values within self._position--don't replace it.
2929+
for num,val in zip(pos, self._position):
2930+
val.set(num)
2931+
if which in ('both', 'original'):
2932+
self._originalPosition = pos
29222933

29232934
def stem(self, x, y, linefmt='b-', markerfmt='bo', basefmt='r-'):
29242935
"""
@@ -3948,25 +3959,24 @@ def dist(a):
39483959
ds.sort()
39493960
return ds[0][1]
39503961

3951-
def set_aspect(self, aspect='auto', fixLimits=None,
3952-
aspect_adjusts='position'):
3962+
def set_aspect(self, aspect='auto', adjusts='position'):
39533963
"""
39543964
aspect:
39553965
'auto' - automatic; fill position rectangle with data
39563966
'normal' - same as 'auto'; deprecated
39573967
'equal' - same scaling from data to plot units for x and y
3958-
A - a circle will be stretched such that the height
3959-
is A times the width. aspect=1 is the same as
3968+
num - a circle will be stretched such that the height
3969+
is num times the width. aspect=1 is the same as
39603970
aspect='equal'.
39613971
3962-
aspect_adjusts:
3972+
adjusts:
39633973
'position' - change width or height of bounding rectangle;
39643974
keep it centered.
39653975
'box_size' - as above, but anchored to lower left
39663976
'datalim' - change xlim or ylim
39673977
3968-
fixLimits: deprecated; False is aspect_adjusts='datalim';
3969-
True is aspect_adjusts='position'
3978+
Note: the 'adjusts' argument is a convenience; it can be set
3979+
independently by set_aspect_adjusts.
39703980
39713981
ACCEPTS: ['auto' | 'equal' | aspect_ratio]
39723982
"""
@@ -3977,22 +3987,20 @@ def set_aspect(self, aspect='auto', fixLimits=None,
39773987
else:
39783988
self._aspect = float(aspect) # raise ValueError if necessary
39793989

3980-
if fixLimits is not None:
3981-
if not fixLimits: aspect_adjusts = 'datalim'
3982-
if aspect_adjusts in ('position', 'box_size', 'datalim'):
3983-
self._aspect_adjusts = aspect_adjusts
3990+
if adjusts in ('position', 'box_size', 'datalim'):
3991+
self._aspect_adjusts = adjusts
39843992
else:
39853993
raise ValueError(
3986-
'aspect_adjusts must be "position", "box_size", or "datalim"')
3994+
'adjusts must be "position", "box_size", or "datalim"')
39873995

3988-
def set_aspect_adjusts(self, aspect_adjusts = 'position'):
3996+
def set_aspect_adjusts(self, adjusts = 'position'):
39893997
"""
39903998
Must be called after set_aspect.
39913999
39924000
ACCEPTS: ['position' | 'box_size' | 'datalim']
39934001
"""
3994-
if aspect_adjusts in ('position', 'box_size', 'datalim'):
3995-
self._aspect_adjusts = aspect_adjusts
4002+
if adjusts in ('position', 'box_size', 'datalim'):
4003+
self._aspect_adjusts = adjusts
39964004
else:
39974005
raise ValueError(
39984006
'argument must be "position", "box_size", or "datalim"')
@@ -4005,7 +4013,7 @@ def apply_aspect(self):
40054013
'''
40064014

40074015
if self._aspect == 'auto':
4008-
self.set_position( self._originalPosition )
4016+
self.set_position( self._originalPosition , 'active')
40094017
return
40104018

40114019
if self._aspect == 'equal':
@@ -4034,10 +4042,9 @@ def apply_aspect(self):
40344042
B = b + 0.5 * (h-H)
40354043
else:
40364044
L,B = l,b
4037-
self.set_position((L,B,W,H))
4045+
self.set_position((L,B,W,H), 'active')
40384046
return
40394047

4040-
self.autoscale_view()
40414048
xmin,xmax = self.get_xlim()
40424049
xsize = math.fabs(xmax-xmin)
40434050
ymin,ymax = self.get_ylim()

lib/matplotlib/backend_bases.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ def format_deltas(event,dx,dy):
13331333
else:
13341334
ymin = lasty+alphay*(ymin-lasty)
13351335
ymax = lasty+alphay*(ymax-lasty)
1336-
1336+
13371337
a.set_xlim((xmin, xmax))
13381338
a.set_ylim((ymin, ymax))
13391339

@@ -1354,15 +1354,15 @@ def release_zoom(self, event):
13541354
self.release(event)
13551355
self.draw()
13561356
return
1357-
1357+
13581358
xmin, ymin, xmax, ymax = lim
1359-
1359+
13601360
# zoom to rect
13611361
lastx, lasty = a.transData.inverse_xy_tup( (lastx, lasty) )
13621362
x, y = a.transData.inverse_xy_tup( (x, y) )
13631363
Xmin,Xmax=a.get_xlim()
13641364
Ymin,Ymax=a.get_ylim()
1365-
1365+
13661366
if Xmin < Xmax:
13671367
if x<lastx: xmin, xmax = x, lastx
13681368
else: xmin, xmax = lastx, x
@@ -1373,7 +1373,7 @@ def release_zoom(self, event):
13731373
else: xmin, xmax = lastx, x
13741374
if xmin > Xmin: xmin=Xmin
13751375
if xmax < Xmax: xmax=Xmax
1376-
1376+
13771377
if Ymin < Ymax:
13781378
if y<lasty: ymin, ymax = y, lasty
13791379
else: ymin, ymax = lasty, y
@@ -1384,7 +1384,7 @@ def release_zoom(self, event):
13841384
else: ymin, ymax = lasty, y
13851385
if ymin > Ymin: ymin=Ymin
13861386
if ymax < Ymax: ymax=Ymax
1387-
1387+
13881388
if self._button_pressed == 1:
13891389
a.set_xlim((xmin, xmax))
13901390
a.set_ylim((ymin, ymax))
@@ -1409,14 +1409,15 @@ def release_zoom(self, event):
14091409
a.set_ylim((y1, y2))
14101410

14111411
# Zoom with fixed aspect; modified for shared x-axes
1412-
aspect = a.get_aspect()
1413-
if aspect == 'equal' or aspect == 'scaled':
1414-
self.fix_aspect_after_zoom(a)
1415-
else:
1416-
aspect_shared = ''
1417-
if a._sharex != None: aspect_shared = a._sharex.get_aspect()
1418-
if aspect_shared == 'equal' or aspect_shared == 'scaled':
1419-
self.fix_aspect_after_zoom(a._sharex)
1412+
if 0:
1413+
aspect = a.get_aspect()
1414+
if aspect == 'equal' or aspect == 'scaled':
1415+
self.fix_aspect_after_zoom(a)
1416+
else:
1417+
aspect_shared = ''
1418+
if a._sharex != None: aspect_shared = a._sharex.get_aspect()
1419+
if aspect_shared == 'equal' or aspect_shared == 'scaled':
1420+
self.fix_aspect_after_zoom(a._sharex)
14201421

14211422
self.draw()
14221423
self._xypress = None
@@ -1429,7 +1430,7 @@ def fix_aspect_after_zoom(self,a):
14291430
'Fix the aspect ratio after zooming in case of aspect equal or scaled'
14301431
lold,bold,wold,hold = a.get_position()
14311432
aspect = a.get_aspect()
1432-
a.set_aspect(aspect,True)
1433+
a.set_aspect_adjusts('datalim')
14331434
l,b,w,h = a.get_position()
14341435
if w != wold: # width of axes was changed
14351436
ratio = w / wold
@@ -1461,7 +1462,9 @@ def draw(self):
14611462

14621463

14631464
def _update_view(self):
1464-
'update the viewlim and position from the view and position stack for each axes'
1465+
'''update the viewlim and position from the view and
1466+
position stack for each axes
1467+
'''
14651468

14661469
lims = self._views()
14671470
if lims is None: return

lib/matplotlib/figure.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,11 +717,11 @@ def colorbar(self, mappable, cax=None,
717717
l,b,w,h = ax.get_position()
718718
if orientation=='vertical':
719719
neww = 0.8*w
720-
ax.set_position((l,b,neww,h))
720+
ax.set_position((l,b,neww,h), 'both')
721721
cax = self.add_axes([l + 0.9*w, b, 0.1*w, h])
722722
else:
723723
newh = 0.8*h
724-
ax.set_position((l,b+0.2*h,w,newh))
724+
ax.set_position((l,b+0.2*h,w,newh), 'both')
725725
cax = self.add_axes([l, b, w, 0.1*h])
726726

727727
else:

0 commit comments

Comments
 (0)