Skip to content

Commit 0c7b471

Browse files
committed
New QuiverKey class and quiverkey function; deprecated quiver_classic.
svn path=/trunk/matplotlib/; revision=2466
1 parent 8640d64 commit 0c7b471

File tree

10 files changed

+276
-78
lines changed

10 files changed

+276
-78
lines changed

CHANGELOG

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
2006-06-11 Fixed a usetex bug for windows, running latex on files
1+
2006-06-11 Added quiverkey command to pylab and Axes, using
2+
QuiverKey class in quiver.py. Changed pylab and Axes
3+
to use quiver2 if possible, but drop back to the
4+
newly-renamed quiver_classic if necessary. Modified
5+
examples/quiver_demo.py to illustrate the new quiver
6+
and quiverkey. Changed LineCollection implementation
7+
slightly to improve compatibility with PolyCollection. - EF
8+
9+
2006-06-11 Fixed a usetex bug for windows, running latex on files
210
with spaces in their names or paths was failing - DSD
311

412
2006-06-09 Made additions to numerix, changes to quiver to make it

boilerplate.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def %(func)s(*args, **kwargs):
8484
'vlines',
8585
'quiver',
8686
'quiver2',
87+
'quiverkey',
8788
)
8889

8990
_misccommands = (
@@ -103,6 +104,7 @@ def %(func)s(*args, **kwargs):
103104
'imshow' : 'gci._current = ret',
104105
'spy2' : 'gci._current = ret',
105106
'quiver2' : 'gci._current = ret',
107+
'quiver' : 'gci._current = ret',
106108
'specgram' : 'gci._current = ret[-1]',
107109

108110
}

examples/quiver2_demo.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

examples/quiver_demo.py

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,69 @@
1+
'''
2+
Demonstration of quiver and quiverkey functions. This is using the
3+
new version coming from the code in quiver.py.
14
5+
Known problem: the plot autoscaling does not take into account
6+
the arrows, so those on the boundaries are often out of the picture.
7+
This is *not* an easy problem to solve in a perfectly general way.
8+
The workaround is to manually expand the axes.
9+
10+
'''
211
from pylab import *
312

413
X,Y = meshgrid( arange(0,2*pi,.2),arange(0,2*pi,.2) )
514
U = cos(X)
615
V = sin(Y)
716

17+
#1
818
figure()
9-
quiver( X, Y, U, V, 0.2, color='length')
19+
Q = quiver( U, V)
20+
qk = quiverkey(Q, 0.5, 0.92, 2, '2 m/s', labelpos='W',
21+
fontproperties={'weight': 'bold'})
22+
l,r,b,t = axis()
23+
dx, dy = r-l, t-b
24+
axis([l-0.05*dx, r+0.05*dx, b-0.05*dy, t+0.05*dy])
25+
26+
title('Minimal arguments, no kwargs')
1027

28+
#2
1129
figure()
12-
quiver( U, V, 0.8, color='r' )
30+
Q = quiver( X, Y, U, V, units='width')
31+
qk = quiverkey(Q, 0.9, 0.95, 2, '2 m/s',
32+
labelpos='E',
33+
coordinates='figure',
34+
fontproperties={'weight': 'bold'})
35+
axis([-1, 7, -1, 7])
36+
title('scales with plot width, not view')
1337

38+
#3
1439
figure()
15-
quiver( U, V)
16-
title('quiver(U,V)')
40+
Q = quiver( X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3],
41+
pivot='mid', color='r', units='inches' )
42+
qk = quiverkey(Q, 0.5, 0.03, 1, '1 m/s', fontproperties={'weight': 'bold'})
43+
plot( X[::3, ::3], Y[::3, ::3], 'k.')
44+
axis([-1, 7, -1, 7])
45+
title("pivot='mid'; every third arrow; units='inches'")
1746

47+
#4
1848
figure()
19-
quiver( U, V, color=U+V )
49+
M = sqrt(pow(U, 2) + pow(V, 2))
50+
Q = quiver( X, Y, U, V, M, units='x', pivot='tip', width=0.022, scale=1/0.15)
51+
qk = quiverkey(Q, 0.9, 1.05, 1, '1 m/s',
52+
labelpos='E',
53+
fontproperties={'weight': 'bold'})
54+
plot(X, Y, 'k.')
55+
axis([-1, 7, -1, 7])
56+
title("scales with x view; pivot='tip'")
2057

58+
#5
2159
figure()
22-
quiver( X, Y, U, V )
23-
title('quiver(X,Y,U,V); needs a scale parameter')
60+
Q = quiver( X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3],
61+
color='r', units='x',
62+
linewidths=(2,), edgecolors=('k'), headaxislength=5 )
63+
qk = quiverkey(Q, 0.5, 0.03, 1, '1 m/s', fontproperties={'weight': 'bold'})
64+
axis([-1, 7, -1, 7])
65+
title("triangular head; scale with x view; black edges")
66+
67+
2468
show()
2569

lib/matplotlib/axes.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
from cbook import iterable, is_string_like, flatten, enumerate, \
1717
allequal, dict_delall, popd, popall, silent_list
1818
from collections import RegularPolyCollection, PolyCollection, LineCollection, QuadMesh
19-
from colors import colorConverter, normalize, Colormap, LinearSegmentedColormap, looks_like_color
19+
from colors import colorConverter, normalize, Colormap, \
20+
LinearSegmentedColormap, looks_like_color, is_color_like
2021
import cm
2122
from cm import ScalarMappable
2223
from contour import ContourSet
@@ -43,7 +44,7 @@
4344
from transforms import PBox
4445
from font_manager import FontProperties
4546

46-
from quiver import Quiver
47+
from quiver import Quiver, QuiverKey
4748

4849
import matplotlib
4950

@@ -3201,6 +3202,12 @@ def arrow(self, x, y, dx, dy, **kwargs):
32013202
self.add_artist(a)
32023203
return a
32033204

3205+
def quiverkey(self, *args, **kw):
3206+
qk = QuiverKey(*args, **kw)
3207+
self.add_artist(qk)
3208+
return qk
3209+
quiverkey.__doc__ = QuiverKey.quiverkey_doc
3210+
32043211
def quiver2(self, *args, **kw):
32053212
q = Quiver(self, *args, **kw)
32063213
self.add_collection(q)
@@ -3209,7 +3216,21 @@ def quiver2(self, *args, **kw):
32093216
return q
32103217
quiver2.__doc__ = Quiver.quiver_doc
32113218

3212-
def quiver(self, U, V, *args, **kwargs ):
3219+
def quiver(self, *args, **kw):
3220+
if (len(args) == 3 or len(args) == 5) and not iterable(args[-1]):
3221+
return self.quiver_classic(*args, **kw)
3222+
c = kw.get('color', None)
3223+
if c is not None:
3224+
try:
3225+
if not is_color_like(c):
3226+
assert shape(asarray(c)) == shape(asarray(args[-1]))
3227+
return self.quiver_classic(*args, **kw)
3228+
except:
3229+
pass
3230+
return self.quiver2(*args, **kw)
3231+
quiver.__doc__ = Quiver.quiver_doc
3232+
3233+
def quiver_classic(self, U, V, *args, **kwargs ):
32133234
"""
32143235
QUIVER( X, Y, U, V )
32153236
QUIVER( U, V )
@@ -3238,6 +3259,10 @@ def quiver(self, U, V, *args, **kwargs ):
32383259
32393260
32403261
"""
3262+
msg = '''This version of quiver is obsolete and will be
3263+
phased out; please use the new quiver.
3264+
'''
3265+
warnings.warn(msg, DeprecationWarning)
32413266
if not self._hold: self.cla()
32423267
do_scale = True
32433268
S = 1.0

lib/matplotlib/collections.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ class LineCollection(Collection, ScalarMappable):
379379
the len of props is less than the number of sements
380380
"""
381381
zorder = 2
382-
def __init__(self, segments,
382+
def __init__(self, segments, # Can be None.
383383
linewidths = None,
384384
colors = None,
385385
antialiaseds = None,
@@ -436,21 +436,31 @@ def __init__(self, segments,
436436
if antialiaseds is None :
437437
antialiaseds = (rcParams['lines.antialiased'], )
438438

439-
self._segments = list(segments)
440439
self._colors = colorConverter.to_rgba_list(colors)
441440
self._aa = antialiaseds
442441
self._lw = linewidths
443442
self.set_linestyle(linestyle)
443+
self._uniform_offsets = None
444444
if transOffset is None:
445445
if offsets is not None:
446-
self._add_offsets(offsets)
446+
self._uniform_offsets = offsets
447447
offsets = None
448448
transOffset = identity_transform()
449449
self._offsets = offsets
450450
self._transOffset = transOffset
451+
self.set_segments(segments)
452+
453+
def set_segments(self, segments):
454+
if segments is None: return
455+
self._segments = list(segments)
456+
if self._uniform_offsets is not None:
457+
self._add_offsets()
458+
459+
set_verts = set_segments # for compatibility with PolyCollection
451460

452-
def _add_offsets(self, offsets):
461+
def _add_offsets(self):
453462
segs = self._segments
463+
offsets = self._uniform_offsets
454464
Nsegs = len(segs)
455465
Noffs = len(offsets)
456466
if not iterable(offsets[0]): # i.e., not a tuple but an x-offset

lib/matplotlib/colors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,8 @@ def to_rgb(self, arg, warn=True):
391391
arg = tuple(arg)
392392
try: self.cache[arg]
393393
except KeyError: pass
394+
except TypeError:
395+
raise ValueError('to_rgb: unhashable even inside a tuple')
394396

395397
try:
396398
if is_string_like(arg):

lib/matplotlib/pylab.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2265,7 +2265,7 @@ def quiver(*args, **kwargs):
22652265
except:
22662266
hold(b)
22672267
raise
2268-
2268+
gci._current = ret
22692269
hold(b)
22702270
return ret
22712271
if Axes.quiver.__doc__ is not None:
@@ -2293,6 +2293,27 @@ def quiver2(*args, **kwargs):
22932293
quiver2.__doc__ = _shift_string(Axes.quiver2.__doc__) + """
22942294
Addition kwargs: hold = [True|False] overrides default hold state"""
22952295

2296+
# This function was autogenerated by boilerplate.py. Do not edit as
2297+
# changes will be lost
2298+
def quiverkey(*args, **kwargs):
2299+
# allow callers to override the hold state by passing hold=True|False
2300+
b = ishold()
2301+
h = popd(kwargs, 'hold', None)
2302+
if h is not None:
2303+
hold(h)
2304+
try:
2305+
ret = gca().quiverkey(*args, **kwargs)
2306+
draw_if_interactive()
2307+
except:
2308+
hold(b)
2309+
raise
2310+
2311+
hold(b)
2312+
return ret
2313+
if Axes.quiverkey.__doc__ is not None:
2314+
quiverkey.__doc__ = _shift_string(Axes.quiverkey.__doc__) + """
2315+
Addition kwargs: hold = [True|False] overrides default hold state"""
2316+
22962317
# This function was autogenerated by boilerplate.py. Do not edit as
22972318
# changes will be lost
22982319
def cla(*args, **kwargs):
@@ -2526,3 +2547,4 @@ def winter():
25262547

25272548

25282549

2550+

0 commit comments

Comments
 (0)