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

Skip to content

Commit 2e2f850

Browse files
committed
More tweaks to aspect-ratio handling; new Axes.axis method; moved
PBox to transforms.py. svn path=/trunk/matplotlib/; revision=2249
1 parent 9d085c4 commit 2e2f850

File tree

6 files changed

+275
-198
lines changed

6 files changed

+275
-198
lines changed

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2006-04-02 Moved PBox class to transforms and deleted pbox.py;
2+
made pylab axis command a thin wrapper for Axes.axis;
3+
more tweaks to aspect-ratio handling; fixed Axes.specgram
4+
to account for the new imshow default of unit aspect
5+
ratio; made contour set the Axes.dataLim. - EF
6+
17
2006-03-31 Fixed the Qt "Underlying C/C++ object deleted" bug. - JRE
28

39
2006-03-31 Applied Vasily Sulatskov's Qt Navigation Toolbar enhancement. - JRE

lib/matplotlib/axes.py

Lines changed: 99 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
from transforms import FuncXY, Func, LOG10, IDENTITY, POLAR
4141
from transforms import get_bbox_transform, unit_bbox, one, origin, zero
4242
from transforms import blend_xy_sep_transform, Interval
43+
from transforms import PBox
4344
from font_manager import FontProperties
44-
from pbox import PBox
4545

4646
import matplotlib
4747

@@ -478,38 +478,117 @@ def apply_aspect(self, data_ratio = None):
478478
self.set_position(pb1.anchor(self._anchor), 'active')
479479
return
480480

481+
482+
l,b,w,h = self.get_position()
483+
box_aspect = fig_aspect * (h/w)
484+
data_ratio = box_aspect / A
485+
dL = self.dataLim
486+
xr = dL.width()
487+
yr = dL.height()
488+
xmarg = xsize - xr
489+
ymarg = ysize - yr
490+
Ysize = data_ratio * xsize
491+
Xsize = ysize / data_ratio
492+
# If it is very nearly correct, don't do any more;
493+
# we are probably just panning.
494+
if abs(xsize-Xsize) < 0.001*xsize or abs(ysize-Ysize) < 0.001*ysize:
495+
return
496+
481497
changex = ((self._sharey or self._mastery) and not
482498
(self._sharex or self._masterx))
483499
changey = ((self._sharex or self._masterx) and not
484500
(self._sharey or self._mastery))
485501
if changex and changey:
486502
warnings.warn("adjustable='datalim' cannot work with shared x and y axes")
487503
return
488-
dx0, dx1 = self.dataLim.intervalx().get_bounds()
489-
dy0, dy1 = self.dataLim.intervaly().get_bounds()
490-
xr = abs(dx1 - dx0)
491-
yr = abs(dy1 - dy0)
492-
l,b,w,h = self.get_position()
493-
box_aspect = fig_aspect * (h/w)
494-
data_ratio = box_aspect / A
495-
Ysize = data_ratio * xsize
496-
Xsize = ysize / data_ratio
497504
if changex:
498505
adjust_y = False
499506
else:
500-
adjust_y = changey or ((Ysize > ysize) and (ysize <= yr))
507+
adjust_y = changey or (Xsize < min(xsize, xr))
508+
adjust_y = adjust_y or (A*ymarg > xmarg)
501509
if adjust_y:
502-
dy = 0.5 * (Ysize - ysize)
503-
self.set_ylim((ymin-dy, ymax+dy))
510+
dy = Ysize - ysize
511+
if ymarg < 0.01 * dy:
512+
y0 = ymin - dy/2.0
513+
y1 = ymax + dy/2.0
514+
else:
515+
yc = (dL.ymax() + dL.ymin())/2.0
516+
y0 = yc - Ysize/2.0
517+
y1 = yc + Ysize/2.0
518+
self.set_ylim((y0, y1))
504519
else:
505-
dx = 0.5 * (Xsize - xsize)
506-
self.set_xlim((xmin-dx, xmax+dx))
520+
dx = Xsize - xsize
521+
if xmarg < 0.01 * dx:
522+
x0 = xmin - dx/2.0
523+
x1 = xmax + dx/2.0
524+
else:
525+
xc = (dL.xmax() + dL.xmin())/2.0
526+
x0 = xc - Xsize/2.0
527+
x1 = xc + Xsize/2.0
528+
self.set_xlim((x0, x1))
507529

508530
def get_aspect(self):
509531
return self._aspect
510532

511-
def get_aspect_adjusts(self):
512-
return self._aspect_adjusts
533+
def get_adjustable(self):
534+
return self._adjustable
535+
536+
def get_anchor(self):
537+
return self._anchor
538+
539+
def axis(self, *v, **kwargs):
540+
'''
541+
Convenience method for manipulating the x and y view limits
542+
and the aspect ratio of the plot.
543+
544+
See docstring for pylab.axis.
545+
'''
546+
if len(v)==1 and is_string_like(v[0]):
547+
s = v[0].lower()
548+
if s=='on': self.set_axis_on()
549+
elif s=='off': self.set_axis_off()
550+
elif s in ('equal', 'tight', 'scaled', 'normal', 'auto', 'image'):
551+
self.set_autoscale_on(True)
552+
self.set_aspect('auto')
553+
self.autoscale_view()
554+
self.apply_aspect()
555+
if s=='equal':
556+
self.set_aspect('equal', adjustable='datalim')
557+
elif s == 'scaled':
558+
self.set_aspect('equal', adjustable='box', anchor='C')
559+
elif s=='tight':
560+
self.autoscale_view(tight=True)
561+
self.set_autoscale_on(False)
562+
elif s == 'image':
563+
self.autoscale_view(tight=True)
564+
self.set_autoscale_on(False)
565+
self.set_aspect('equal', adjustable='box', anchor='C')
566+
567+
else:
568+
raise ValueError('Unrecognized string %s to axis; try on or off' % s)
569+
xmin, xmax = self.get_xlim()
570+
ymin, ymax = self.get_ylim()
571+
#draw_if_interactive()
572+
return xmin, xmax, ymin, ymax
573+
574+
try: v[0]
575+
except IndexError:
576+
xmin, xmax = self.set_xlim(**kwargs)
577+
ymin, ymax = self.set_ylim(**kwargs)
578+
#draw_if_interactive()
579+
return xmin, xmax, ymin, ymax
580+
581+
v = v[0]
582+
if len(v) != 4:
583+
raise ValueError('v must contain [xmin xmax ymin ymax]')
584+
585+
586+
self.set_xlim([v[0], v[1]])
587+
self.set_ylim([v[2], v[3]])
588+
589+
#draw_if_interactive()
590+
return v
591+
513592

514593
def set_cursor_props(self, *args):
515594
"""
@@ -869,7 +948,7 @@ def get_yscale(self):
869948

870949
def update_datalim(self, xys):
871950
'Update the data lim bbox with seq of xy tups'
872-
# if no data is set currently, the bbox will ignore it's
951+
# if no data is set currently, the bbox will ignore its
873952
# limits and set the bound to be the bounds of the xydata.
874953
# Otherwise, it will compute the bounds of it's current data
875954
# and the data in xydata
@@ -3489,7 +3568,7 @@ def set_xscale(self, value, basex = 10, subsx=None):
34893568
34903569
* subsx: the location of the minor ticks; None defaults to
34913570
autosubs, which depend on the number of decades in the
3492-
plot.
3571+
plot.
34933572
34943573
ACCEPTS: ['log' | 'linear' ]
34953574
"""
@@ -3699,6 +3778,7 @@ def specgram(self, x, NFFT=256, Fs=2, detrend=detrend_none,
36993778
xmin, xmax = xextent
37003779
extent = xmin, xmax, 0, amax(freqs)
37013780
im = self.imshow(Z, cmap, extent=extent)
3781+
self.axis('auto')
37023782

37033783
return Pxx, freqs, bins, im
37043784

lib/matplotlib/contour.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,17 @@ def __init__(self, ax, *args, **kwargs):
463463
self.collections.append(col)
464464

465465
## check: seems like set_xlim should also be inside
466-
if not self.ax.ishold():
467-
self.ax.cla()
468-
self.ax.set_xlim((ma.minimum(x), ma.maximum(x)))
469-
self.ax.set_ylim((ma.minimum(y), ma.maximum(y)))
470-
471-
466+
#if not self.ax.ishold():
467+
# self.ax.cla()
468+
#self.ax.set_xlim((ma.minimum(x), ma.maximum(x)))
469+
#self.ax.set_ylim((ma.minimum(y), ma.maximum(y)))
470+
x0 = ma.minimum(x)
471+
x1 = ma.maximum(x)
472+
y0 = ma.minimum(y)
473+
y1 = ma.maximum(y)
474+
self.ax.update_datalim([(x0,y0), (x1,y1)])
475+
self.ax.set_xlim((x0, x1))
476+
self.ax.set_ylim((y0, y1))
472477

473478
def changed(self):
474479
tcolors = [ (tuple(rgba),) for rgba in

lib/matplotlib/pbox.py

Lines changed: 0 additions & 128 deletions
This file was deleted.

lib/matplotlib/pylab.py

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -610,54 +610,11 @@ def axis(*v, **kwargs):
610610
611611
"""
612612
ax = gca()
613-
if len(v)==1 and is_string_like(v[0]):
614-
s = v[0].lower()
615-
if s=='on': ax.set_axis_on()
616-
elif s=='off': ax.set_axis_off()
617-
elif s in ('equal', 'tight', 'scaled', 'normal', 'auto', 'image'):
618-
ax.set_autoscale_on(True)
619-
ax.set_aspect('auto')
620-
ax.autoscale_view()
621-
ax.apply_aspect()
622-
if s=='equal':
623-
ax.set_aspect('equal', adjustable='datalim')
624-
elif s == 'scaled':
625-
ax.set_aspect('equal', adjustable='box', anchor='C')
626-
elif s=='tight':
627-
ax.autoscale_view(tight=True)
628-
ax.set_autoscale_on(False)
629-
elif s == 'image':
630-
ax.autoscale_view(tight=True)
631-
ax.set_autoscale_on(False)
632-
ax.set_aspect('equal', adjustable='box', anchor='C')
633-
634-
else:
635-
raise ValueError('Unrecognized string %s to axis; try on or off' % s)
636-
xmin, xmax = ax.get_xlim()
637-
ymin, ymax = ax.get_ylim()
638-
draw_if_interactive()
639-
return xmin, xmax, ymin, ymax
640-
641-
try: v[0]
642-
except IndexError:
643-
xmin, xmax = ax.set_xlim(**kwargs)
644-
ymin, ymax = ax.set_ylim(**kwargs)
645-
draw_if_interactive()
646-
return [xmin, xmax, ymin, ymax]
647-
648-
v = v[0]
649-
if len(v) != 4:
650-
raise ValueError('v must contain [xmin xmax ymin ymax]')
651-
652-
653-
ax.set_xlim([v[0], v[1]])
654-
ax.set_ylim([v[2], v[3]])
655-
#if ax.get_aspect() == 'equal':
656-
# ax.set_adjustable('datalim')
657-
613+
v = ax.axis(*v, **kwargs)
658614
draw_if_interactive()
659615
return v
660616

617+
661618
def axes(*args, **kwargs):
662619
"""
663620
Add an axes at positon rect specified by::

0 commit comments

Comments
 (0)