Skip to content

Commit 2788e55

Browse files
committed
Numpified collections
svn path=/trunk/matplotlib/; revision=3411
1 parent 8cdd9ea commit 2788e55

File tree

1 file changed

+69
-71
lines changed

1 file changed

+69
-71
lines changed

lib/matplotlib/collections.py

Lines changed: 69 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,18 @@
88
line segemnts)
99
"""
1010
import math, warnings
11-
from matplotlib import rcParams, verbose
12-
13-
import artist
14-
from artist import Artist, kwdocd
15-
from backend_bases import GraphicsContextBase
16-
from cbook import is_string_like, iterable, dedent
17-
from colors import colorConverter
18-
from cm import ScalarMappable
19-
from numerix import arange, sin, cos, pi, asarray, sqrt, array, newaxis, ones
20-
from numerix import isnan, any, resize
21-
from transforms import identity_transform
22-
11+
import numpy as npy
12+
import matplotlib as mpl
13+
import matplotlib.cbook as cbook
14+
import matplotlib.colors as _colors # avoid conflict with kwarg
15+
import matplotlib.cm as cm
16+
import matplotlib.transforms as transforms
17+
import matplotlib.artist as artist
18+
import matplotlib.backend_bases as backend_bases
2319
import matplotlib.nxutils as nxutils
2420

25-
class Collection(Artist):
21+
22+
class Collection(artist.Artist):
2623
"""
2724
All properties in a collection must be sequences. The
2825
property of the ith element of the collection is the
@@ -37,7 +34,7 @@ class Collection(Artist):
3734
"""
3835

3936
def __init__(self):
40-
Artist.__init__(self)
37+
artist.Artist.__init__(self)
4138

4239

4340
def get_verts(self):
@@ -47,7 +44,7 @@ def get_verts(self):
4744
def _get_value(self, val):
4845
try: return (float(val), )
4946
except TypeError:
50-
if iterable(val) and len(val):
47+
if cbook.iterable(val) and len(val):
5148
try: float(val[0])
5249
except TypeError: pass # raise below
5350
else: return val
@@ -58,16 +55,16 @@ def _get_value(self, val):
5855
# these are not available for the object inspector until after the
5956
# class is built so we define an initial set here for the init
6057
# function and they will be overridden after object defn
61-
kwdocd['PatchCollection'] = """\
58+
artist.kwdocd['PatchCollection'] = """\
6259
Valid PatchCollection kwargs are:
6360
6461
edgecolors=None,
6562
facecolors=None,
6663
linewidths=None,
6764
antialiaseds = None,
6865
offsets = None,
69-
transOffset = identity_transform(),
70-
norm = None, # optional for ScalarMappable
66+
transOffset = transforms.identity_transform(),
67+
norm = None, # optional for cm.ScalarMappable
7168
cmap = None, # ditto
7269
7370
offsets and transOffset are used to translate the patch after
@@ -78,7 +75,7 @@ def _get_value(self, val):
7875
form.
7976
"""
8077

81-
class PatchCollection(Collection, ScalarMappable):
78+
class PatchCollection(Collection, cm.ScalarMappable):
8279
"""
8380
Base class for filled regions such as PolyCollection etc.
8481
It must be subclassed to be usable.
@@ -90,8 +87,8 @@ class PatchCollection(Collection, ScalarMappable):
9087
linewidths=None,
9188
antialiaseds = None,
9289
offsets = None,
93-
transOffset = identity_transform(),
94-
norm = None, # optional for ScalarMappable
90+
transOffset = transforms.identity_transform(),
91+
norm = None, # optional for cm.ScalarMappable
9592
cmap = None, # ditto
9693
9794
offsets and transOffset are used to translate the patch after
@@ -123,27 +120,27 @@ def __init__(self,
123120
%(PatchCollection)s
124121
"""
125122
Collection.__init__(self)
126-
ScalarMappable.__init__(self, norm, cmap)
123+
cm.ScalarMappable.__init__(self, norm, cmap)
127124

128-
if facecolors is None: facecolors = rcParams['patch.facecolor']
129-
if edgecolors is None: edgecolors = rcParams['patch.edgecolor']
130-
if linewidths is None: linewidths = (rcParams['patch.linewidth'],)
131-
if antialiaseds is None: antialiaseds = (rcParams['patch.antialiased'],)
125+
if facecolors is None: facecolors = mpl.rcParams['patch.facecolor']
126+
if edgecolors is None: edgecolors = mpl.rcParams['patch.edgecolor']
127+
if linewidths is None: linewidths = (mpl.rcParams['patch.linewidth'],)
128+
if antialiaseds is None: antialiaseds = (mpl.rcParams['patch.antialiased'],)
132129

133-
self._facecolors = colorConverter.to_rgba_list(facecolors)
130+
self._facecolors = _colors.colorConverter.to_rgba_list(facecolors)
134131
if edgecolors == 'None':
135132
self._edgecolors = self._facecolors
136133
linewidths = (0,)
137134
else:
138-
self._edgecolors = colorConverter.to_rgba_list(edgecolors)
135+
self._edgecolors = _colors.colorConverter.to_rgba_list(edgecolors)
139136
self._linewidths = linewidths
140137
self._antialiaseds = antialiaseds
141138
#self._offsets = offsets
142139
self._offsets = offsets
143140
self._transOffset = transOffset
144141
self._verts = []
145142

146-
__init__.__doc__ = dedent(__init__.__doc__) % kwdocd
143+
__init__.__doc__ = cbook.dedent(__init__.__doc__) % artist.kwdocd
147144

148145
def pick(self, mouseevent):
149146
"""
@@ -186,7 +183,7 @@ def get_transformed_patches(self):
186183
for i in xrange(N):
187184
#print 'i%%Nverts=%d'%(i%Nverts)
188185
polyverts = verts[i % Nverts]
189-
if any(isnan(polyverts)):
186+
if npy.any(npy.isnan(polyverts)):
190187
continue
191188
#print 'thisvert', i, polyverts
192189
tverts = transform.seq_xy_tups(polyverts)
@@ -200,7 +197,7 @@ def get_transformed_patches(self):
200197

201198
def get_transoffset(self):
202199
if self._transOffset is None:
203-
self._transOffset = identity_transform()
200+
self._transOffset = transforms.identity_transform()
204201
return self._transOffset
205202

206203

@@ -233,7 +230,7 @@ def set_facecolor(self, c):
233230
234231
ACCEPTS: matplotlib color arg or sequence of rgba tuples
235232
"""
236-
self._facecolors = colorConverter.to_rgba_list(c)
233+
self._facecolors = _colors.colorConverter.to_rgba_list(c)
237234

238235
def set_edgecolor(self, c):
239236
"""
@@ -243,7 +240,7 @@ def set_edgecolor(self, c):
243240
244241
ACCEPTS: matplotlib color arg or sequence of rgba tuples
245242
"""
246-
self._edgecolors = colorConverter.to_rgba_list(c)
243+
self._edgecolors = _colors.colorConverter.to_rgba_list(c)
247244

248245
def set_alpha(self, alpha):
249246
"""
@@ -255,9 +252,9 @@ def set_alpha(self, alpha):
255252
try: float(alpha)
256253
except TypeError: raise TypeError('alpha must be a float')
257254
else:
258-
Artist.set_alpha(self, alpha)
255+
artist.Artist.set_alpha(self, alpha)
259256
self._facecolors = [(r,g,b,alpha) for r,g,b,a in self._facecolors]
260-
if is_string_like(self._edgecolors) and self._edgecolors != 'None':
257+
if cbook.is_string_like(self._edgecolors) and self._edgecolors != 'None':
261258
self._edgecolors = [(r,g,b,alpha) for r,g,b,a in self._edgecolors]
262259

263260
def update_scalarmappable(self):
@@ -336,7 +333,7 @@ def __init__(self, verts, **kwargs):
336333
"""
337334
PatchCollection.__init__(self,**kwargs)
338335
self._verts = verts
339-
__init__.__doc__ = dedent(__init__.__doc__) % kwdocd
336+
__init__.__doc__ = cbook.dedent(__init__.__doc__) % artist.kwdocd
340337

341338
def set_verts(self, verts):
342339
'''This allows one to delay initialization of the vertices.'''
@@ -352,7 +349,7 @@ def draw(self, renderer):
352349
transform.freeze()
353350
transoffset.freeze()
354351
self.update_scalarmappable()
355-
if is_string_like(self._edgecolors) and self._edgecolors[:2] == 'No':
352+
if cbook.is_string_like(self._edgecolors) and self._edgecolors[:2] == 'No':
356353
self._linewidths = (0,)
357354
#self._edgecolors = self._facecolors
358355
renderer.draw_poly_collection(
@@ -398,7 +395,7 @@ def __init__(self, xranges, yrange, **kwargs):
398395
ymax = ymin + ywidth
399396
verts = [ [(xmin, ymin), (xmin, ymax), (xmin+xwidth, ymax), (xmin+xwidth, ymin)] for xmin, xwidth in xranges]
400397
PolyCollection.__init__(self, verts, **kwargs)
401-
__init__.__doc__ = dedent(__init__.__doc__) % kwdocd
398+
__init__.__doc__ = cbook.dedent(__init__.__doc__) % artist.kwdocd
402399

403400
class RegularPolyCollection(PatchCollection):
404401
def __init__(self,
@@ -448,30 +445,30 @@ def __init__(self,
448445
self.numsides = numsides
449446
self.rotation = rotation
450447
self._update_verts()
451-
__init__.__doc__ = dedent(__init__.__doc__) % kwdocd
448+
__init__.__doc__ = cbook.dedent(__init__.__doc__) % artist.kwdocd
452449

453450
def get_transformed_patches(self):
454451
# Shouldn't need all these calls to asarray;
455452
# the variables should be converted when stored.
456453
# Similar speedups with numerix should be attainable
457454
# in many other places.
458-
verts = asarray(self._verts)
459-
offsets = asarray(self._offsets)
455+
verts = npy.asarray(self._verts)
456+
offsets = npy.asarray(self._offsets)
460457
Npoly = len(offsets)
461-
scales = sqrt(asarray(self._sizes)*self._dpi.get()/72.0)
458+
scales = npy.sqrt(npy.asarray(self._sizes)*self._dpi.get()/72.0)
462459
Nscales = len(scales)
463460
if Nscales >1:
464-
scales = resize(scales, (Npoly, 1, 1))
461+
scales = npy.resize(scales, (Npoly, 1, 1))
465462
transOffset = self.get_transoffset()
466463
xyo = transOffset.numerix_xy(offsets)
467-
polys = scales * verts + xyo[:, newaxis, :]
464+
polys = scales * verts + xyo[:, npy.newaxis, :]
468465
return polys
469466

470467

471468
def _update_verts(self):
472469
r = 1.0/math.sqrt(math.pi) # unit area
473-
theta = (2*math.pi/self.numsides)*arange(self.numsides) + self.rotation
474-
self._verts = zip( r*sin(theta), r*cos(theta) )
470+
theta = (2*math.pi/self.numsides)*npy.arange(self.numsides) + self.rotation
471+
self._verts = zip( r*npy.sin(theta), r*npy.cos(theta) )
475472

476473
def draw(self, renderer):
477474
if not self.get_visible(): return
@@ -483,7 +480,7 @@ def draw(self, renderer):
483480
transoffset.freeze()
484481
self.update_scalarmappable()
485482
self._update_verts()
486-
scales = sqrt(asarray(self._sizes)*self._dpi.get()/72.0)
483+
scales = npy.sqrt(npy.asarray(self._sizes)*self._dpi.get()/72.0)
487484

488485

489486
offsets = self._offsets
@@ -499,7 +496,7 @@ def draw(self, renderer):
499496
#print 'drawing offsets', offsets
500497
#print 'drawing verts', self._verts
501498
#print 'drawing scales', scales
502-
if is_string_like(self._edgecolors) and self._edgecolors[:2] == 'No':
499+
if cbook.is_string_like(self._edgecolors) and self._edgecolors[:2] == 'No':
503500
#self._edgecolors = self._facecolors
504501
self._linewidths = (0,)
505502
renderer.draw_regpoly_collection(
@@ -548,16 +545,17 @@ def __init__(self,
548545
"""
549546

550547
RegularPolyCollection.__init__(self, dpi, numsides, rotation, sizes, **kwargs)
551-
__init__.__doc__ = dedent(__init__.__doc__) % kwdocd
548+
__init__.__doc__ = cbook.dedent(__init__.__doc__) % artist.kwdocd
552549

553550
def _update_verts(self):
554551
scale = 1.0/math.sqrt(math.pi)
555-
r = scale*ones(self.numsides*2)
552+
ns2 = self.numsides*2
553+
r = scale*npy.ones(ns2)
556554
r[1::2] *= 0.5
557-
theta = (2.*math.pi/(2*self.numsides))*arange(2*self.numsides) + self.rotation
558-
self._verts = zip( r*sin(theta), r*cos(theta) )
555+
theta = (2.*math.pi/(ns2))*npy.arange(ns2) + self.rotation
556+
self._verts = zip( r*npy.sin(theta), r*npy.cos(theta) )
559557

560-
class LineCollection(Collection, ScalarMappable):
558+
class LineCollection(Collection, cm.ScalarMappable):
561559
"""
562560
All parameters must be sequences. The property of the ith line
563561
segment is the prop[i % len(props)], ie the properties cycle if
@@ -566,11 +564,11 @@ class LineCollection(Collection, ScalarMappable):
566564
zorder = 2
567565
def __init__(self, segments, # Can be None.
568566
linewidths = None,
569-
colors = None,
567+
colors = None,
570568
antialiaseds = None,
571569
linestyle = 'solid',
572570
offsets = None,
573-
transOffset = None,#identity_transform(),
571+
transOffset = None,#transforms.identity_transform(),
574572
norm = None,
575573
cmap = None,
576574
**kwargs
@@ -590,7 +588,7 @@ def __init__(self, segments, # Can be None.
590588
solid|dashed|dashdot|dotted. The dash tuple is (offset, onoffseq)
591589
where onoffseq is an even length tuple of on and off ink in points.
592590
593-
If linewidths, colors, or antialiaseds is None, they default to
591+
If linewidths, colors_, or antialiaseds is None, they default to
594592
their rc params setting, in sequence form.
595593
596594
If offsets and transOffset are not None, then
@@ -613,43 +611,43 @@ def __init__(self, segments, # Can be None.
613611
"""
614612

615613
Collection.__init__(self)
616-
ScalarMappable.__init__(self, norm, cmap)
614+
cm.ScalarMappable.__init__(self, norm, cmap)
617615

618616
if linewidths is None :
619-
linewidths = (rcParams['lines.linewidth'], )
617+
linewidths = (mpl.rcParams['lines.linewidth'], )
620618

621619
if colors is None :
622-
colors = (rcParams['lines.color'],)
620+
colors = (mpl.rcParams['lines.color'],)
623621
if antialiaseds is None :
624-
antialiaseds = (rcParams['lines.antialiased'], )
622+
antialiaseds = (mpl.rcParams['lines.antialiased'], )
625623

626-
self._colors = colorConverter.to_rgba_list(colors)
624+
self._colors = _colors.colorConverter.to_rgba_list(colors)
627625
self._aa = antialiaseds
628626
self._lw = linewidths
629627
self.set_linestyle(linestyle)
630628
self._uniform_offsets = None
631629
if offsets is not None:
632-
offsets = asarray(offsets)
630+
offsets = npy.asarray(offsets)
633631
if len(offsets.shape) == 1:
634-
offsets = offsets[newaxis,:] # Make it Nx2.
632+
offsets = offsets[npy.newaxis,:] # Make it Nx2.
635633
if transOffset is None:
636634
if offsets is not None:
637635
self._uniform_offsets = offsets
638636
offsets = None
639-
transOffset = identity_transform()
637+
transOffset = transforms.identity_transform()
640638
self._offsets = offsets
641639
self._transOffset = transOffset
642640
self.set_segments(segments)
643641
self.update(kwargs)
644642

645643
def get_transoffset(self):
646644
if self._transOffset is None:
647-
self._transOffset = identity_transform()
645+
self._transOffset = transforms.identity_transform()
648646
return self._transOffset
649647

650648
def set_segments(self, segments):
651649
if segments is None: return
652-
self._segments = [asarray(seg) for seg in segments]
650+
self._segments = [npy.asarray(seg) for seg in segments]
653651
if self._uniform_offsets is not None:
654652
self._add_offsets()
655653

@@ -720,9 +718,9 @@ def set_linestyle(self, ls):
720718
Set the linestyles(s) for the collection.
721719
ACCEPTS: ['solid' | 'dashed', 'dashdot', 'dotted' | (offset, on-off-dash-seq) ]
722720
"""
723-
if is_string_like(ls):
724-
dashes = GraphicsContextBase.dashd[ls]
725-
elif iterable(ls) and len(ls)==2:
721+
if cbook.is_string_like(ls):
722+
dashes = backend_bases.GraphicsContextBase.dashd[ls]
723+
elif cbook.iterable(ls) and len(ls)==2:
726724
dashes = ls
727725
else: raise ValueError('Do not know how to convert %s to dashes'%ls)
728726

@@ -738,7 +736,7 @@ def set_color(self, c):
738736
739737
ACCEPTS: matplotlib color arg or sequence of rgba tuples
740738
"""
741-
self._colors = colorConverter.to_rgba_list(c)
739+
self._colors = _colors.colorConverter.to_rgba_list(c)
742740

743741
def color(self, c):
744742
"""
@@ -764,7 +762,7 @@ def set_alpha(self, alpha):
764762
try: float(alpha)
765763
except TypeError: raise TypeError('alpha must be a float')
766764
else:
767-
Artist.set_alpha(self, alpha)
765+
artist.Artist.set_alpha(self, alpha)
768766
self._colors = [(r,g,b,alpha) for r,g,b,a in self._colors]
769767

770768
def get_linewidth(self):

0 commit comments

Comments
 (0)