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

Skip to content

Commit da94b46

Browse files
committed
Use numerix more in collections; quiver handles masked arrays.
svn path=/trunk/matplotlib/; revision=2622
1 parent b733c6a commit da94b46

4 files changed

Lines changed: 37 additions & 30 deletions

File tree

lib/matplotlib/backend_bases.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from cbook import is_string_like, enumerate, strip_math, Stack
1010
from colors import colorConverter
1111
from numerix import array, sqrt, pi, log, asarray, ones, zeros, Float, Float32
12-
from numerix import arange, compress, take
12+
from numerix import arange, compress, take, isnan, any
1313
from patches import Rectangle
1414
from transforms import lbwh_to_bbox, identity_transform
1515
import widgets
@@ -92,7 +92,7 @@ def draw_line_collection(self, segments, transform, clipbox,
9292
matplotlib.collections for more details.
9393
9494
segments is a sequence of ( line0, line1, line2), where linen =
95-
(x0, y0), (x1, y1), ... (xm, ym). Each line can be a
95+
is an Mx2 array with columns x, y. Each line can be a
9696
different length
9797
9898
transform is used to Transform the lines
@@ -110,8 +110,9 @@ def draw_line_collection(self, segments, transform, clipbox,
110110
antialiseds is a tuple of ones or zeros indicating whether the
111111
segment should be aa or not
112112
113-
offsets, if not None, is a list of x,y offsets to translate the lines
114-
by after transform is used to transform the offset coords
113+
offsets, if not None, is an Nx2 array of x,y offsets to
114+
translate the lines by after transform is used to transform
115+
the offset coords
115116
116117
This function could be overridden in the backend to possibly implement
117118
faster drawing, but it is already much faster than using draw_lines()
@@ -133,7 +134,8 @@ def draw_line_collection(self, segments, transform, clipbox,
133134
usingOffsets = offsets is not None
134135
Noffsets = 0
135136
if usingOffsets:
136-
Noffsets = len(offsets)
137+
Noffsets = offsets.shape[0]
138+
offsets = transOffset.numerix_xy(offsets)
137139

138140
for i in xrange(max(Noffsets, Nsegments)):
139141
color = colors[i % Nc]
@@ -146,16 +148,12 @@ def draw_line_collection(self, segments, transform, clipbox,
146148
gc.set_antialiased( antialiaseds[i % Naa] )
147149
seg = segments[i % Nsegments]
148150
if not len(seg): continue
149-
x, y = zip(*seg)
150-
151-
x, y = transform.numerix_x_y(array(x), array(y))
151+
xy = transform.numerix_xy(seg)
152152
if usingOffsets:
153-
xo, yo = transOffset.xy_tup(offsets[i % Noffsets])
154-
x += xo
155-
y += yo
153+
xy = xy + offsets[i % Noffsets]
156154

157-
if newstyle: self.draw_lines(gc, x, y, identity)
158-
else: self.draw_lines(gc, x, y)
155+
if newstyle: self.draw_lines(gc, xy[:,0], xy[:,1], identity)
156+
else: self.draw_lines(gc, xy[:,0], xy[:,1])
159157

160158
def draw_line(self, gc, x1, y1, x2, y2):
161159
"""
@@ -241,7 +239,9 @@ def draw_poly_collection(
241239
gc.set_clip_rectangle(clipbox.get_bounds())
242240

243241
for i in xrange(N):
244-
242+
polyverts = verts[i % Nverts]
243+
if any(isnan(polyverts)):
244+
continue
245245
linewidth = linewidths[i % Nlw]
246246
rf,gf,bf,af = facecolors[i % Nface]
247247
re,ge,be,ae = edgecolors[i % Nedge]
@@ -265,7 +265,7 @@ def draw_poly_collection(
265265

266266
gc.set_antialiased( antialiaseds[i % Naa] ) # Used for fill only?
267267
gc.set_alpha( alpha )
268-
tverts = transform.seq_xy_tups(verts[i % Nverts])
268+
tverts = transform.seq_xy_tups(polyverts)
269269
if usingOffsets:
270270
xo,yo = transOffset.xy_tup(offsets[i % Noffsets])
271271
tverts = [(x+xo,y+yo) for x,y in tverts]

lib/matplotlib/collections.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from cbook import is_string_like, iterable
1616
from colors import colorConverter
1717
from cm import ScalarMappable
18-
from numerix import arange, sin, cos, pi, asarray, sqrt, array
18+
from numerix import arange, sin, cos, pi, asarray, sqrt, array, newaxis
1919
from transforms import identity_transform
2020

2121
class Collection(Artist):
@@ -393,7 +393,8 @@ def __init__(self, segments, # Can be None.
393393
):
394394
"""
395395
segments is a sequence of ( line0, line1, line2), where
396-
linen = (x0, y0), (x1, y1), ... (xm, ym).
396+
linen = (x0, y0), (x1, y1), ... (xm, ym), or the
397+
equivalent numerix array with two columns.
397398
Each line can be a different length.
398399
399400
colors must be a tuple of RGBA tuples (eg arbitrary color
@@ -443,6 +444,10 @@ def __init__(self, segments, # Can be None.
443444
self._lw = linewidths
444445
self.set_linestyle(linestyle)
445446
self._uniform_offsets = None
447+
if offsets is not None:
448+
offsets = asarray(offsets)
449+
if len(offsets.shape) == 1:
450+
offsets = offsets[newaxis,:] # Make it Nx2.
446451
if transOffset is None:
447452
if offsets is not None:
448453
self._uniform_offsets = offsets
@@ -454,7 +459,7 @@ def __init__(self, segments, # Can be None.
454459

455460
def set_segments(self, segments):
456461
if segments is None: return
457-
self._segments = list(segments)
462+
self._segments = [asarray(seg) for seg in segments]
458463
if self._uniform_offsets is not None:
459464
self._add_offsets()
460465

@@ -464,15 +469,14 @@ def _add_offsets(self):
464469
segs = self._segments
465470
offsets = self._uniform_offsets
466471
Nsegs = len(segs)
467-
Noffs = len(offsets)
468-
if not iterable(offsets[0]): # i.e., not a tuple but an x-offset
469-
xo, yo = offsets
472+
Noffs = offsets.shape[0]
473+
if Noffs == 1:
470474
for i in range(Nsegs):
471-
segs[i] = [(x+xo*i, y+yo*i) for x,y in segs[i]]
475+
segs[i] = segs[i] + i * offsets
472476
else:
473477
for i in range(Nsegs):
474-
xo, yo = offsets[i%Noffs]
475-
segs[i] = [(x+xo, y+yo) for x,y in segs[i]]
478+
io = i%Noffs
479+
segs[i] = segs[i] + offsets[io:io+1]
476480

477481

478482
def draw(self, renderer):

lib/matplotlib/quiver.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ def _parse_args(self, *args):
296296
if len(args) == 3 or len(args) == 5:
297297
C = nx.ravel(args.pop(-1))
298298
#print 'in parse_args, C:', C
299-
V = nx.asarray(args.pop(-1))
300-
U = nx.asarray(args.pop(-1))
299+
V = nx.ma.asarray(args.pop(-1))
300+
U = nx.ma.asarray(args.pop(-1))
301301
nn = nx.shape(U)
302302
nc = nn[0]
303303
nr = 1
@@ -333,8 +333,8 @@ def draw(self, renderer):
333333
PolyCollection.draw(self, renderer)
334334

335335
def set_UVC(self, U, V, C=None):
336-
self.U = nx.ravel(U)
337-
self.V = nx.ravel(V)
336+
self.U = nx.ma.ravel(U)
337+
self.V = nx.ma.ravel(V)
338338
if C is not None:
339339
self.set_array(nx.ravel(C))
340340
self._new_UV = True
@@ -367,6 +367,7 @@ def _set_transform(self):
367367

368368
def _make_verts(self, U, V):
369369
uv = U+V*1j
370+
uv = nx.ravel(nx.ma.filled(uv, nx.nan))
370371
a = nx.absolute(uv)
371372
if self.scale is None:
372373
sn = max(10, math.sqrt(self.N))
@@ -379,7 +380,6 @@ def _make_verts(self, U, V):
379380
xy = xy[:,:,nx.newaxis]
380381
XY = nx.concatenate((xy.real, xy.imag), axis=2)
381382
return XY
382-
#return [zip(xyrow.real, xyrow.imag) for xyrow in xy]
383383

384384

385385
def _h_arrows(self, length):

src/_transforms.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,14 +961,17 @@ Transformation::numerix_xy(const Py::Tuple & args) {
961961
size_t Nxy = xyin->dimensions[0];
962962
size_t N2 = xyin->dimensions[1];
963963

964-
if (N2!=2)
964+
if (N2!=2) {
965+
Py_XDECREF(xyin);
965966
throw Py::ValueError("xy must have shape (N,2)");
967+
}
966968

967969
// evaluate the lazy objects
968970
try {
969971
if (!_frozen) eval_scalars();
970972
}
971973
catch(...) {
974+
Py_XDECREF(xyin);
972975
throw Py::ValueError("Domain error on Transformation::numerix_xy");
973976
}
974977

0 commit comments

Comments
 (0)