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

Skip to content

Commit 71d803d

Browse files
committed
Milestone -- simple_plot.py working with new affine framework (with
the exception of dpi propagation) svn path=/branches/transforms/; revision=3839
1 parent 36ab0f9 commit 71d803d

4 files changed

Lines changed: 89 additions & 50 deletions

File tree

lib/matplotlib/affine.py

Lines changed: 88 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,61 +33,48 @@ def __init__(self, points):
3333
self._points = N.asarray(points, N.float_)
3434
self.track = False
3535

36-
# JDH: if you define a del method, the garbage collector won't
37-
# destory cyclic references, so make sure you either manage these
38-
# yourself or remove the __del__ after testing
39-
def __del__(self):
40-
if self.track:
41-
print "Bbox::__del__"
42-
4336
#@staticmethod
4437
def unit():
45-
return Bbox([[0,0], [1,1]])
38+
return Bbox.from_lbrt(0., 0., 1., 1.)
4639
unit = staticmethod(unit)
4740

4841
#@staticmethod
4942
def from_lbwh(left, bottom, width, height):
50-
return Bbox([[left, bottom], [left + width, bottom + height]])
43+
return Bbox.from_lbrt(left, bottom, left + width, bottom + height)
5144
from_lbwh = staticmethod(from_lbwh)
5245

5346
#@staticmethod
54-
def from_lbrt(left, bottom, right, top):
55-
return Bbox([[left, bottom], [right, top]])
47+
def from_lbrt(*args):
48+
points = N.array(args, dtype=N.float_).reshape(2, 2)
49+
return Bbox(points)
5650
from_lbrt = staticmethod(from_lbrt)
5751

58-
52+
def __cmp__(self, other):
53+
# MGDTODO: Totally suboptimal
54+
if isinstance(other, Bbox):
55+
if (self._points == other._points).all():
56+
return 0
57+
return -1
58+
5959
# JDH: the update method will update the box limits from the
6060
# existing limits and the new data; it appears here you are just
6161
# using the new data. We use an "ignore" flag to specify whether
6262
# you want to include the existing data or not in the update
6363
def update_from_data(self, x, y):
6464
self._points = N.array([[x.min(), y.min()], [x.max(), y.max()]], N.float_)
6565
self.invalidate()
66-
if self.track:
67-
print "Bbox::update_from_data", self._points
6866

6967
def copy(self):
70-
if self.track:
71-
print "Bbox::copy"
7268
return Bbox(self._points.copy())
7369

7470
def __repr__(self):
7571
return 'Bbox(%s)' % repr(self._points)
7672
__str__ = __repr__
7773

78-
def __cmp__(self, other):
79-
# MGDTODO: Totally suboptimal
80-
if isinstance(other, Bbox):
81-
return (self._points == other._points).all()
82-
return -1
83-
8474
# MGDTODO: Probably a more efficient ways to do this...
8575
def _get_xmin(self):
86-
if self.track:
87-
print "Bbox::_get_xmin"
8876
return self._points[0, 0]
8977
def _set_xmin(self, val):
90-
print "Bbox::_set_xmin"
9178
self._points[0, 0] = val
9279
self.invalidate()
9380
xmin = property(_get_xmin, _set_xmin)
@@ -150,10 +137,10 @@ def _get_height(self):
150137
height = property(_get_height)
151138

152139
def transformed(self, transform):
153-
return Bbox(self.transform(self._points))
140+
return Bbox(transform(self._points))
154141

155142
def inverse_transformed(self, transform):
156-
return Bbox(self.transform.inverted()(self._points))
143+
return Bbox(transform.inverted()(self._points))
157144

158145
def get_bounds(self):
159146
return (self.xmin, self.ymin,
@@ -249,6 +236,14 @@ def __repr__(self):
249236
return "Affine2D(%s)" % repr(self._mtx)
250237
__str__ = __repr__
251238

239+
def __cmp__(self, other):
240+
# MGDTODO: We need to decide if we want deferred transforms
241+
# to be equal to this one
242+
if isinstance(other, Affine2D):
243+
if (self.get_matrix() == other.get_matrix()).all():
244+
return 0
245+
return -1
246+
252247
def _do_invalidation(self):
253248
result = self._inverted is None
254249
self._inverted = None
@@ -380,10 +375,9 @@ def _make__mtx(self):
380375
if self._mtx is None:
381376
x_mtx = self._x.get_matrix()
382377
y_mtx = self._y.get_matrix()
378+
# This works because we already know the transforms are
379+
# separable
383380
self._mtx = N.vstack([x_mtx[0], y_mtx[1], [0.0, 0.0, 1.0]])
384-
# self._mtx = self.matrix_from_values(
385-
# x_mtx[0,0], 0.0, 0.0, y_mtx[1,1], x_mtx[0,2], y_mtx[1,2])
386-
print "Blended", x_mtx, y_mtx, self._mtx
387381

388382
def is_separable(self):
389383
return True
@@ -429,8 +423,8 @@ def _do_invalidation(self):
429423
def _make__mtx(self):
430424
if self._mtx is None:
431425
self._mtx = self._concat(
432-
self._b.get_matrix(),
433-
self._a.get_matrix())
426+
self._a.get_matrix(),
427+
self._b.get_matrix())
434428

435429
def get_matrix(self):
436430
self._make__mtx()
@@ -547,12 +541,70 @@ def interval_contains_open(interval, val):
547541
return interval[0] < val and interval[1] > val
548542

549543
if __name__ == '__main__':
544+
bbox = Bbox.from_lbrt(10., 15., 20., 25.)
545+
assert bbox.xmin == 10
546+
assert bbox.ymin == 15
547+
assert bbox.xmax == 20
548+
assert bbox.ymax == 25
549+
550+
assert N.all(bbox.min == [10, 15])
551+
assert N.all(bbox.max == [20, 25])
552+
assert N.all(bbox.intervalx == (10, 20))
553+
assert N.all(bbox.intervaly == (15, 25))
554+
555+
assert bbox.width == 10
556+
assert bbox.height == 10
557+
558+
assert bbox.get_bounds() == (10, 15, 10, 10)
559+
560+
bbox.intervalx = (11, 21)
561+
bbox.intervaly = (16, 26)
562+
563+
assert bbox.get_bounds() == (11, 16, 10, 10)
564+
565+
bbox.xmin = 12
566+
bbox.ymin = 17
567+
bbox.xmax = 22
568+
bbox.ymax = 27
569+
570+
assert bbox.get_bounds() == (12, 17, 10, 10)
571+
572+
bbox = Bbox.from_lbwh(10, 11, 12, 13)
573+
assert bbox.get_bounds() == (10, 11, 12, 13)
574+
575+
bbox_copy = bbox.copy()
576+
assert bbox == bbox_copy
577+
bbox_copy.max = (14, 15)
578+
assert bbox.get_bounds() == (10, 11, 12, 13)
579+
assert bbox_copy.get_bounds() == (10, 11, 4, 4)
580+
550581
bbox1 = Bbox([[10., 15.], [20., 25.]])
551582
bbox2 = Bbox([[30., 35.], [40., 45.]])
552583
trans = BboxTransform(bbox1, bbox2)
553-
print trans(bbox1._points)
554-
555-
bbox2.intervalx = 50, 55
556-
print trans(bbox1._points)
584+
bbox3 = bbox1.transformed(trans)
585+
assert bbox3 == bbox2
586+
587+
translation = Affine2D().translate(10, 20)
588+
assert translation.to_values() == (1, 0, 0, 1, 10, 20)
589+
scale = Affine2D().scale(10, 20)
590+
assert scale.to_values() == (10, 0, 0, 20, 0, 0)
591+
rotation = Affine2D().rotate_deg(30)
592+
print rotation.to_values() == (0.86602540378443871, 0.49999999999999994, -0.49999999999999994, 0.86602540378443871, 0.0, 0.0)
593+
594+
points = N.array([[1,2],[3,4],[5,6],[7,8]], N.float_)
595+
translated_points = translation(points)
596+
assert (translated_points == [[11., 22.], [13., 24.], [15., 26.], [17., 28.]]).all()
597+
scaled_points = scale(points)
598+
print scaled_points
599+
rotated_points = rotation(points)
600+
print rotated_points
601+
602+
tpoints1 = rotation(translation(scale(points)))
603+
trans_sum = rotation + translation + scale
604+
tpoints2 = trans_sum(points)
605+
print tpoints1, tpoints2
606+
print tpoints1 == tpoints2
607+
# Need to do some sort of fuzzy comparison here?
608+
# assert (tpoints1 == tpoints2).all()
557609

558610
__all__ = ['Transform', 'Affine2D']

lib/matplotlib/axes.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -653,15 +653,12 @@ def _set_lim_and_transforms(self):
653653

654654
self.viewLim = Bbox.from_lbrt(left, bottom, right, top)
655655
self.dataLim = Bbox.unit()
656-
self.dataLim.track = True
657656

658657
self.transData = maffine.BboxTransform(
659658
self.viewLim, self.bbox)
660659
self.transAxes = maffine.BboxTransform(
661660
Bbox.unit(), self.bbox)
662661

663-
print "_set_lim_and_transforms", self.viewLim, self.transData, self.dataLim, self.transAxes, self.bbox
664-
665662
# MGDTODO
666663
# if self._sharex:
667664
# self.transData.set_funcx(self._sharex.transData.get_funcx())
@@ -697,7 +694,6 @@ def set_position(self, pos, which='both'):
697694
# # Change values within self._position--don't replace it.
698695
# for num,val in zip(pos, self._position):
699696
# val.set(num)
700-
print "set_position", self._position, pos
701697
self._position = pos
702698
# MGDTODO: side-effects
703699
if which in ('both', 'original'):
@@ -1182,9 +1178,7 @@ def update_datalim_numerix(self, x, y):
11821178
#print type(x), type(y)
11831179
# MGDTODO
11841180
## self.dataLim.update_numerix(x, y, -1)
1185-
print "update_datalim_numerix", self.dataLim,
11861181
self.dataLim.update_from_data(x, y)
1187-
print self.dataLim
11881182

11891183
def _get_verts_in_data_coords(self, trans, xys):
11901184
if trans == self.transData:
@@ -1245,8 +1239,6 @@ def autoscale_view(self, tight=False, scalex=True, scaley=True):
12451239
axis direction reversal that has already been done.
12461240
"""
12471241
# if image data only just use the datalim
1248-
print "autoscale_view", self._autoscaleon, scalex, scaley
1249-
12501242
if not self._autoscaleon: return
12511243
if (tight or (len(self.images)>0 and
12521244
len(self.lines)==0 and
@@ -1274,7 +1266,7 @@ def autoscale_view(self, tight=False, scalex=True, scaley=True):
12741266

12751267
def draw(self, renderer=None, inframe=False):
12761268
"Draw everything (plot lines, axes, labels)"
1277-
if renderer is None:
1269+
if renderer is None:
12781270
renderer = self._cachedRenderer
12791271

12801272
if renderer is None:
@@ -1550,7 +1542,6 @@ def set_xlim(self, xmin=None, xmax=None, emit=True, **kwargs):
15501542
xmin, xmax = maffine.nonsingular(xmin, xmax, increasing=False)
15511543

15521544
self.viewLim.intervalx = (xmin, xmax)
1553-
print 'set_xlim', self.viewLim, xmin, xmax
15541545

15551546
return xmin, xmax
15561547

@@ -1654,7 +1645,6 @@ def set_ylim(self, ymin=None, ymax=None, emit=True, **kwargs):
16541645
ACCEPTS: len(2) sequence of floats
16551646
"""
16561647

1657-
print "set_ylim", ymin, ymax, emit
16581648
if ymax is None and iterable(ymin):
16591649
ymin,ymax = ymin
16601650

@@ -1676,7 +1666,6 @@ def set_ylim(self, ymin=None, ymax=None, emit=True, **kwargs):
16761666
ymin, ymax = maffine.nonsingular(ymin, ymax, increasing=False)
16771667
self.viewLim.intervaly = (ymin, ymax)
16781668
if emit: self.callbacks.process('ylim_changed', self)
1679-
print "set_ylim", self.viewLim
16801669

16811670
return ymin, ymax
16821671

lib/matplotlib/figure.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@ def set_size_inches(self, *args, **kwargs):
326326

327327
dpival = self.dpi
328328
self.bbox.max = w * dpival, h * dpival
329-
print self.bbox
330329
# self.figwidth.set(w) MGDTODO
331330
# self.figheight.set(h)
332331

lib/matplotlib/lines.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,6 @@ def get_window_extent(self, renderer):
389389

390390

391391
def set_axes(self, ax):
392-
print "set_axes"
393392
Artist.set_axes(self, ax)
394393
if ax.xaxis is not None:
395394
self._xcid = ax.xaxis.callbacks.connect('units', self.recache)

0 commit comments

Comments
 (0)