Skip to content

Commit dac9540

Browse files
committed
Speedup RegularPolyCollection.get_transformed_patches
svn path=/trunk/matplotlib/; revision=3263
1 parent bdbaf73 commit dac9540

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

lib/matplotlib/collections.py

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from colors import colorConverter
1818
from cm import ScalarMappable
1919
from numerix import arange, sin, cos, pi, asarray, sqrt, array, newaxis, ones
20-
from numerix import isnan, any
20+
from numerix import isnan, any, resize
2121
from transforms import identity_transform
2222

2323
import matplotlib.nxutils as nxutils
@@ -141,7 +141,7 @@ def __init__(self,
141141
#self._offsets = offsets
142142
self._offsets = offsets
143143
self._transOffset = transOffset
144-
self._verts = []
144+
self._verts = []
145145

146146
__init__.__doc__ = dedent(__init__.__doc__) % kwdocd
147147

@@ -153,13 +153,13 @@ def pick(self, mouseevent):
153153
if not self.pickable(): return
154154
ind = []
155155
x, y = mouseevent.x, mouseevent.y
156-
for i, thispoly in enumerate(self.get_transformed_patches()):
156+
for i, thispoly in enumerate(self.get_transformed_patches()):
157157
inside = nxutils.pnpoly(x, y, thispoly)
158158
if inside: ind.append(i)
159159
if len(ind):
160160
self.figure.canvas.pick_event(mouseevent, self, ind=ind)
161-
162-
161+
162+
163163
def get_transformed_patches(self):
164164
"""
165165
get a sequence of the polygons in the collection in display (transformed) space
@@ -348,7 +348,7 @@ def draw(self, renderer):
348348
transform = self.get_transform()
349349
transoffset = self.get_transoffset()
350350

351-
351+
352352
transform.freeze()
353353
transoffset.freeze()
354354
self.update_scalarmappable()
@@ -364,14 +364,14 @@ def draw(self, renderer):
364364
transoffset.thaw()
365365
renderer.close_group('polycollection')
366366

367-
367+
368368
def get_verts(self, dataTrans=None):
369369
'''Return vertices in data coordinates.
370370
The calculation is incomplete in general; it is based
371371
on the vertices or the offsets, whichever is using
372372
dataTrans as its transformation, so it does not take
373373
into account the combined effect of segments and offsets.
374-
'''
374+
'''
375375
verts = []
376376
if self._offsets is None:
377377
for seg in self._verts:
@@ -451,24 +451,23 @@ def __init__(self,
451451
__init__.__doc__ = dedent(__init__.__doc__) % kwdocd
452452

453453
def get_transformed_patches(self):
454-
455-
xverts, yverts = zip(*self._verts)
456-
xverts = asarray(xverts)
457-
yverts = asarray(yverts)
458-
sizes = sqrt(asarray(self._sizes)*self._dpi.get()/72.0)
459-
Nsizes = len(sizes)
454+
# Shouldn't need all these calls to asarray;
455+
# the variables should be converted when stored.
456+
# Similar speedups with numerix should be attainable
457+
# in many other places.
458+
verts = asarray(self._verts)
459+
offsets = asarray(self._offsets)
460+
Npoly = len(offsets)
461+
scales = sqrt(asarray(self._sizes)*self._dpi.get()/72.0)
462+
Nscales = len(scales)
463+
if Nscales >1:
464+
scales = resize(scales, (Npoly, 1, 1))
460465
transOffset = self.get_transoffset()
461-
polys = []
462-
for i, loc in enumerate(self._offsets):
463-
xo,yo = transOffset.xy_tup(loc)
464-
#print 'xo, yo', loc, (xo, yo)
465-
scale = sizes[i % Nsizes]
466-
467-
thisxverts = scale*xverts + xo
468-
thisyverts = scale*yverts + yo
469-
polys.append(zip(thisxverts, thisyverts))
466+
xyo = transOffset.numerix_xy(offsets)
467+
polys = scales * verts + xyo[:, newaxis, :]
470468
return polys
471469

470+
472471
def _update_verts(self):
473472
r = 1.0/math.sqrt(math.pi) # unit area
474473
theta = (2*math.pi/self.numsides)*arange(self.numsides) + self.rotation
@@ -486,7 +485,7 @@ def draw(self, renderer):
486485
self._update_verts()
487486
scales = sqrt(asarray(self._sizes)*self._dpi.get()/72.0)
488487

489-
488+
490489
offsets = self._offsets
491490
if self._offsets is not None:
492491
xs, ys = zip(*offsets)
@@ -642,7 +641,7 @@ def __init__(self, segments, # Can be None.
642641
self._transOffset = transOffset
643642
self.set_segments(segments)
644643
self.update(kwargs)
645-
644+
646645
def get_transoffset(self):
647646
if self._transOffset is None:
648647
self._transOffset = identity_transform()
@@ -678,7 +677,7 @@ def draw(self, renderer):
678677

679678
transform.freeze()
680679
transoffset.freeze()
681-
680+
682681
segments = self._segments
683682
offsets = self._offsets
684683

@@ -693,7 +692,7 @@ def draw(self, renderer):
693692
xs = self.convert_xunits(self._offsets[:0])
694693
ys = self.convert_yunits(self._offsets[:1])
695694
offsets = zip(xs, ys)
696-
695+
697696
self.update_scalarmappable()
698697
renderer.draw_line_collection(
699698
segments, transform, self.clipbox,

0 commit comments

Comments
 (0)