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

Skip to content

Commit cf3a22e

Browse files
committed
Merged revisions 7616-7618 via svnmerge from
https://matplotlib.svn.sourceforge.net/svnroot/matplotlib/branches/v0_99_maint ........ r7616 | ryanmay | 2009-08-31 12:27:12 -0500 (Mon, 31 Aug 2009) | 1 line Update barb_demo.py with an example using masked arrays. ........ r7617 | ryanmay | 2009-08-31 12:29:41 -0500 (Mon, 31 Aug 2009) | 1 line Pull _parse_args out of Quiver and Barbs classes, as we have multiple, diverging copies of the same code. Now the shared code will actually be shared. ........ r7618 | ryanmay | 2009-08-31 12:34:50 -0500 (Mon, 31 Aug 2009) | 1 line Use atleast_1d instead of asanyarray. This allows passing in scalars to quiver/barbs. ........ svn path=/trunk/matplotlib/; revision=7619
1 parent 19e9bd9 commit cf3a22e

2 files changed

Lines changed: 41 additions & 45 deletions

File tree

examples/pylab_examples/barb_demo.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
ax.barbs(data['x'], data['y'], data['u'], data['v'], length=8, pivot='middle')
2929

3030
#Showing colormapping with uniform grid. Fill the circle for an empty barb,
31-
#don't round the values, and change some of the size parameters
31+
#don't round the values, and change some of the size parameters
3232
ax = plt.subplot(2,2,3)
3333
ax.barbs(X, Y, U, V, np.sqrt(U*U + V*V), fill_empty=True, rounding=False,
3434
sizes=dict(emptybarb=0.25, spacing=0.2, height=0.3))
@@ -39,4 +39,15 @@
3939
barbcolor=['b','g'], barb_increments=dict(half=10, full=20, flag=100),
4040
flip_barb=True)
4141

42+
#Masked arrays are also supported
43+
masked_u = np.ma.masked_array(data['u'])
44+
masked_u[4] = 1000 #Bad value that should not be plotted when masked
45+
masked_u[4] = np.ma.masked
46+
47+
#Identical plot to panel 2 in the first figure, but with the point at
48+
#(0.5, 0.25) missing (masked)
49+
fig2 = plt.figure()
50+
ax = fig2.add_subplot(1, 1, 1)
51+
ax.barbs(data['x'], data['y'], masked_u, data['v'], length=8, pivot='middle')
52+
4253
plt.show()

lib/matplotlib/quiver.py

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,33 @@ def contains(self, mouseevent):
325325
quiverkey_doc = _quiverkey_doc
326326

327327

328+
# This is a helper function that parses out the various combination of
329+
# arguments for doing colored vector plots. Pulling it out here
330+
# allows both Quiver and Barbs to use it
331+
def _parse_args(*args):
332+
X, Y, U, V, C = [None]*5
333+
args = list(args)
334+
335+
# The use of atleast_1d allows for handling scalar arguments while also
336+
# keeping masked arrays
337+
if len(args) == 3 or len(args) == 5:
338+
C = np.atleast_1d(args.pop(-1))
339+
V = np.atleast_1d(args.pop(-1))
340+
U = np.atleast_1d(args.pop(-1))
341+
if U.ndim == 1:
342+
nr, nc = 1, U.shape[0]
343+
else:
344+
nr, nc = U.shape
345+
if len(args) == 2: # remaining after removing U,V,C
346+
X, Y = [np.array(a).ravel() for a in args]
347+
if len(X) == nc and len(Y) == nr:
348+
X, Y = [a.ravel() for a in np.meshgrid(X, Y)]
349+
else:
350+
indexgrid = np.meshgrid(np.arange(nc), np.arange(nr))
351+
X, Y = [np.ravel(a) for a in indexgrid]
352+
return X, Y, U, V, C
353+
354+
328355
class Quiver(collections.PolyCollection):
329356
"""
330357
Specialized PolyCollection for arrows.
@@ -351,7 +378,7 @@ def __init__(self, ax, *args, **kw):
351378
by the following pylab interface documentation:
352379
%s"""
353380
self.ax = ax
354-
X, Y, U, V, C = self._parse_args(*args)
381+
X, Y, U, V, C = _parse_args(*args)
355382
self.X = X
356383
self.Y = Y
357384
self.XY = np.hstack((X[:,np.newaxis], Y[:,np.newaxis]))
@@ -390,26 +417,6 @@ def on_dpi_change(fig):
390417
self.ax.figure.callbacks.connect('dpi_changed', on_dpi_change)
391418

392419

393-
def _parse_args(self, *args):
394-
X, Y, U, V, C = [None]*5
395-
args = list(args)
396-
if len(args) == 3 or len(args) == 5:
397-
C = np.asanyarray(args.pop(-1))
398-
V = np.asanyarray(args.pop(-1))
399-
U = np.asanyarray(args.pop(-1))
400-
if U.ndim == 1:
401-
nr, nc = 1, U.shape[0]
402-
else:
403-
nr, nc = U.shape
404-
if len(args) == 2: # remaining after removing U,V,C
405-
X, Y = [np.array(a).ravel() for a in args]
406-
if len(X) == nc and len(Y) == nr:
407-
X, Y = [a.ravel() for a in np.meshgrid(X, Y)]
408-
else:
409-
indexgrid = np.meshgrid(np.arange(nc), np.arange(nr))
410-
X, Y = [np.ravel(a) for a in indexgrid]
411-
return X, Y, U, V, C
412-
413420
def _init(self):
414421
"""initialization delayed until first draw;
415422
allow time for axes setup.
@@ -762,7 +769,7 @@ def __init__(self, ax, *args, **kw):
762769
kw['facecolors'] = flagcolor
763770

764771
#Parse out the data arrays from the various configurations supported
765-
x, y, u, v, c = self._parse_args(*args)
772+
x, y, u, v, c = _parse_args(*args)
766773
self.x = x
767774
self.y = y
768775
xy = np.hstack((x[:,np.newaxis], y[:,np.newaxis]))
@@ -942,28 +949,6 @@ def _make_barbs(self, u, v, nflags, nbarbs, half_barb, empty_flag, length,
942949

943950
return barb_list
944951

945-
#Taken shamelessly from Quiver
946-
def _parse_args(self, *args):
947-
X, Y, U, V, C = [None]*5
948-
args = list(args)
949-
if len(args) == 3 or len(args) == 5:
950-
C = ma.masked_invalid(args.pop(-1), copy=False).ravel()
951-
V = ma.masked_invalid(args.pop(-1), copy=False)
952-
U = ma.masked_invalid(args.pop(-1), copy=False)
953-
nn = np.shape(U)
954-
nc = nn[0]
955-
nr = 1
956-
if len(nn) > 1:
957-
nr = nn[1]
958-
if len(args) == 2: # remaining after removing U,V,C
959-
X, Y = [np.array(a).ravel() for a in args]
960-
if len(X) == nc and len(Y) == nr:
961-
X, Y = [a.ravel() for a in np.meshgrid(X, Y)]
962-
else:
963-
indexgrid = np.meshgrid(np.arange(nc), np.arange(nr))
964-
X, Y = [np.ravel(a) for a in indexgrid]
965-
return X, Y, U, V, C
966-
967952
def set_UVC(self, U, V, C=None):
968953
self.u = ma.masked_invalid(U, copy=False).ravel()
969954
self.v = ma.masked_invalid(V, copy=False).ravel()

0 commit comments

Comments
 (0)