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

Skip to content

Commit dfb4e9e

Browse files
committed
Merge pull request #7913 from dstansby/quiver-doc
[MRG+2] Clean up quiver docstring + add simple quiver example
1 parent 64fdd33 commit dfb4e9e

File tree

3 files changed

+114
-138
lines changed

3 files changed

+114
-138
lines changed
Lines changed: 16 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'''
2-
Demonstration of quiver and quiverkey functions. This is using the
3-
new version coming from the code in quiver.py.
2+
========================================================
3+
Demonstration of advanced quiver and quiverkey functions
4+
========================================================
45
56
Known problem: the plot autoscaling does not take into account
67
the arrows, so those on the boundaries are often out of the picture.
78
This is *not* an easy problem to solve in a perfectly general way.
89
The workaround is to manually expand the axes.
9-
1010
'''
1111
import matplotlib.pyplot as plt
1212
import numpy as np
@@ -16,81 +16,27 @@
1616
U = np.cos(X)
1717
V = np.sin(Y)
1818

19-
# 1
20-
plt.figure()
21-
Q = plt.quiver(U, V)
22-
qk = plt.quiverkey(Q, 0.5, 0.98, 2, r'$2 \frac{m}{s}$', labelpos='W',
23-
fontproperties={'weight': 'bold'})
24-
l, r, b, t = plt.axis()
25-
dx, dy = r - l, t - b
26-
plt.axis([l - 0.05*dx, r + 0.05*dx, b - 0.05*dy, t + 0.05*dy])
27-
28-
plt.title('Minimal arguments, no kwargs')
29-
30-
# 2
3119
plt.figure()
20+
plt.title('Arrows scale with plot width, not view')
3221
Q = plt.quiver(X, Y, U, V, units='width')
33-
qk = plt.quiverkey(Q, 0.9, 0.95, 2, r'$2 \frac{m}{s}$',
34-
labelpos='E',
35-
coordinates='figure',
36-
fontproperties={'weight': 'bold'})
37-
plt.axis([-1, 7, -1, 7])
38-
plt.title('scales with plot width, not view')
22+
qk = plt.quiverkey(Q, 0.9, 0.9, 2, r'$2 \frac{m}{s}$', labelpos='E',
23+
coordinates='figure')
3924

40-
# 3
4125
plt.figure()
42-
Q = plt.quiver(X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3],
43-
pivot='mid', color='r', units='inches')
44-
qk = plt.quiverkey(Q, 0.5, 0.03, 1, r'$1 \frac{m}{s}$',
45-
fontproperties={'weight': 'bold'})
46-
plt.plot(X[::3, ::3], Y[::3, ::3], 'k.')
47-
plt.axis([-1, 7, -1, 7])
4826
plt.title("pivot='mid'; every third arrow; units='inches'")
27+
Q = plt.quiver(X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3],
28+
pivot='mid', units='inches')
29+
qk = plt.quiverkey(Q, 0.9, 0.9, 1, r'$1 \frac{m}{s}$', labelpos='E',
30+
coordinates='figure')
31+
plt.scatter(X[::3, ::3], Y[::3, ::3], color='r', s=5)
4932

50-
# 4
5133
plt.figure()
34+
plt.title("pivot='tip'; scales with x view")
5235
M = np.hypot(U, V)
53-
Q = plt.quiver(X, Y, U, V, M,
54-
units='x',
55-
pivot='tip',
56-
width=0.022,
36+
Q = plt.quiver(X, Y, U, V, M, units='x', pivot='tip', width=0.022,
5737
scale=1 / 0.15)
58-
qk = plt.quiverkey(Q, 0.9, 1.05, 1, r'$1 \frac{m}{s}$',
59-
labelpos='E',
60-
fontproperties={'weight': 'bold'})
61-
plt.plot(X, Y, 'k.', markersize=2)
62-
plt.axis([-1, 7, -1, 7])
63-
plt.title("scales with x view; pivot='tip'")
64-
65-
# 5
66-
plt.figure()
67-
Q = plt.quiver(X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3],
68-
color='r', units='x',
69-
linewidths=(0.5,), edgecolors=('k'), headaxislength=5)
70-
qk = plt.quiverkey(Q, 0.5, 0.03, 1, r'$1 \frac{m}{s}$',
71-
fontproperties={'weight': 'bold'})
72-
plt.axis([-1, 7, -1, 7])
73-
plt.title("triangular head; scale with x view; black edges")
74-
75-
# 6
76-
plt.figure()
77-
M = np.zeros(U.shape, dtype='bool')
78-
XMaskStart = U.shape[0]//3
79-
YMaskStart = U.shape[1]//3
80-
XMaskStop = 2*U.shape[0]//3
81-
YMaskStop = 2*U.shape[1]//3
82-
83-
M[XMaskStart:XMaskStop,
84-
YMaskStart:YMaskStop] = True
85-
U = ma.masked_array(U, mask=M)
86-
V = ma.masked_array(V, mask=M)
87-
Q = plt.quiver(U, V)
88-
qk = plt.quiverkey(Q, 0.5, 0.98, 2, r'$2 \frac{m}{s}$', labelpos='W',
89-
fontproperties={'weight': 'bold'})
90-
l, r, b, t = plt.axis()
91-
dx, dy = r - l, t - b
92-
plt.axis([l - 0.05 * dx, r + 0.05 * dx, b - 0.05 * dy, t + 0.05 * dy])
93-
plt.title('Minimal arguments, no kwargs - masked values')
94-
38+
qk = plt.quiverkey(Q, 0.9, 0.9, 1, r'$1 \frac{m}{s}$', labelpos='E',
39+
coordinates='figure')
40+
plt.scatter(X, Y, color='k', s=5)
9541

9642
plt.show()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
==================================================
3+
A simple example of a quiver plot with a quiverkey
4+
==================================================
5+
'''
6+
import matplotlib.pyplot as plt
7+
import numpy as np
8+
9+
X = np.arange(-10, 10, 1)
10+
Y = np.arange(-10, 10, 1)
11+
U, V = np.meshgrid(X, Y)
12+
13+
fig, ax = plt.subplots()
14+
q = ax.quiver(X, Y, U, V)
15+
ax.quiverkey(q, X=0.3, Y=1.1, U=10,
16+
label='Quiver key, length = 10', labelpos='E')
17+
18+
plt.show()

lib/matplotlib/quiver.py

Lines changed: 80 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -38,128 +38,140 @@
3838
_quiver_doc = """
3939
Plot a 2-D field of arrows.
4040
41-
call signatures::
41+
Call signatures::
4242
4343
quiver(U, V, **kw)
4444
quiver(U, V, C, **kw)
4545
quiver(X, Y, U, V, **kw)
4646
quiver(X, Y, U, V, C, **kw)
4747
48-
Arguments:
49-
50-
*X*, *Y*:
51-
The x and y coordinates of the arrow locations (default is tail of
52-
arrow; see *pivot* kwarg)
53-
54-
*U*, *V*:
55-
Give the x and y components of the arrow vectors
48+
*U* and *V* are the arrow data, *X* and *Y* set the locaiton of the
49+
arrows, and *C* sets the color of the arrows. These arguments may be 1-D or
50+
2-D arrays or sequences.
5651
57-
*C*:
58-
An optional array used to map colors to the arrows
59-
60-
All arguments may be 1-D or 2-D arrays or sequences. If *X* and *Y*
61-
are absent, they will be generated as a uniform grid. If *U* and *V*
62-
are 2-D arrays but *X* and *Y* are 1-D, and if ``len(X)`` and ``len(Y)``
63-
match the column and row dimensions of *U*, then *X* and *Y* will be
52+
If *X* and *Y* are absent, they will be generated as a uniform grid.
53+
If *U* and *V* are 2-D arrays and *X* and *Y* are 1-D, and if ``len(X)`` and
54+
``len(Y)`` match the column and row dimensions of *U*, then *X* and *Y* will be
6455
expanded with :func:`numpy.meshgrid`.
6556
66-
*U*, *V*, *C* may be masked arrays, but masked *X*, *Y* are not
67-
supported at present.
57+
The default settings auto-scales the length of the arrows to a reasonable size.
58+
To change this behavior see the *scale* and *scale_units* kwargs.
6859
69-
Keyword arguments:
60+
The defaults give a slightly swept-back arrow; to make the head a
61+
triangle, make *headaxislength* the same as *headlength*. To make the
62+
arrow more pointed, reduce *headwidth* or increase *headlength* and
63+
*headaxislength*. To make the head smaller relative to the shaft,
64+
scale down all the head parameters. You will probably do best to leave
65+
minshaft alone.
66+
67+
*linewidths* and *edgecolors* can be used to customize the arrow
68+
outlines.
7069
71-
*units*: [ 'width' | 'height' | 'dots' | 'inches' | 'x' | 'y' | 'xy' ]
72-
Arrow units; the arrow dimensions *except for length* are in
73-
multiples of this unit.
70+
Parameters
71+
----------
72+
X : 1D or 2D array, sequence, optional
73+
The x coordinates of the arrow locations
74+
Y : 1D or 2D array, sequence, optional
75+
The y coordinates of the arrow locations
76+
U : 1D or 2D array or masked array, sequence
77+
The x components of the arrow vectors
78+
V : 1D or 2D array or masked array, sequence
79+
The y components of the arrow vectors
80+
C : 1D or 2D array, sequence, optional
81+
The arrow colors
82+
units : [ 'width' | 'height' | 'dots' | 'inches' | 'x' | 'y' | 'xy' ]
83+
The arrow dimensions (except for *length*) are measured in multiples of
84+
this unit.
7485
75-
* 'width' or 'height': the width or height of the axes
86+
'width' or 'height': the width or height of the axis
7687
77-
* 'dots' or 'inches': pixels or inches, based on the figure dpi
88+
'dots' or 'inches': pixels or inches, based on the figure dpi
7889
79-
* 'x', 'y', or 'xy': *X*, *Y*, or sqrt(X^2+Y^2) data units
90+
'x', 'y', or 'xy': respectively *X*, *Y*, or :math:`\sqrt{X^2 + Y^2}`
91+
in data units
8092
8193
The arrows scale differently depending on the units. For
8294
'x' or 'y', the arrows get larger as one zooms in; for other
8395
units, the arrow size is independent of the zoom state. For
8496
'width or 'height', the arrow size increases with the width and
8597
height of the axes, respectively, when the window is resized;
8698
for 'dots' or 'inches', resizing does not change the arrows.
99+
angles : [ 'uv' | 'xy' ], array, optional
100+
Method for determining the angle of the arrows. Default is 'uv'.
87101
88-
89-
*angles*: [ 'uv' | 'xy' | array ]
90-
With the default 'uv', the arrow axis aspect ratio is 1, so that
102+
'uv': the arrow axis aspect ratio is 1 so that
91103
if *U*==*V* the orientation of the arrow on the plot is 45 degrees
92-
CCW from the horizontal axis (positive to the right).
93-
With 'xy', the arrow points from (x,y) to (x+u, y+v).
104+
counter-clockwise from the horizontal axis (positive to the right).
105+
106+
'xy': arrows point from (x,y) to (x+u, y+v).
94107
Use this for plotting a gradient field, for example.
108+
95109
Alternatively, arbitrary angles may be specified as an array
96-
of values in degrees, CCW from the horizontal axis.
110+
of values in degrees, counter-clockwise from the horizontal axis.
111+
97112
Note: inverting a data axis will correspondingly invert the
98-
arrows *only* with `angles='xy'`.
113+
arrows only with ``angles='xy'``.
114+
scale : None, float, optional
115+
Number of data units per arrow length unit, e.g., m/s per plot width; a
116+
smaller scale parameter makes the arrow longer. Default is *None*.
99117
100-
*scale*: [ *None* | float ]
101-
Data units per arrow length unit, e.g., m/s per plot width; a smaller
102-
scale parameter makes the arrow longer. If *None*, a simple
103-
autoscaling algorithm is used, based on the average vector length
104-
and the number of vectors. The arrow length unit is given by
118+
If *None*, a simple autoscaling algorithm is used, based on the average
119+
vector length and the number of vectors. The arrow length unit is given by
105120
the *scale_units* parameter
121+
scale_units : [ 'width' | 'height' | 'dots' | 'inches' | 'x' | 'y' | 'xy' ], \
122+
None, optional
123+
If the *scale* kwarg is *None*, the arrow length unit. Default is *None*.
106124
107-
*scale_units*: *None*, or any of the *units* options.
108-
For example, if *scale_units* is 'inches', *scale* is 2.0, and
125+
e.g. *scale_units* is 'inches', *scale* is 2.0, and
109126
``(u,v) = (1,0)``, then the vector will be 0.5 inches long.
110-
If *scale_units* is 'width', then the vector will be half the width
111-
of the axes.
127+
128+
If *scale_units* is 'width'/'height', then the vector will be half the
129+
width/height of the axes.
112130
113131
If *scale_units* is 'x' then the vector will be 0.5 x-axis
114-
units. To plot vectors in the x-y plane, with u and v having
132+
units. To plot vectors in the x-y plane, with u and v having
115133
the same units as x and y, use
116-
"angles='xy', scale_units='xy', scale=1".
117-
118-
*width*:
134+
``angles='xy', scale_units='xy', scale=1``.
135+
width : scalar, optional
119136
Shaft width in arrow units; default depends on choice of units,
120137
above, and number of vectors; a typical starting value is about
121138
0.005 times the width of the plot.
122-
123-
*headwidth*: scalar
139+
headwidth : scalar, optional
124140
Head width as multiple of shaft width, default is 3
125-
126-
*headlength*: scalar
141+
headlength : scalar, optional
127142
Head length as multiple of shaft width, default is 5
128-
129-
*headaxislength*: scalar
143+
headaxislength : scalar, optional
130144
Head length at shaft intersection, default is 4.5
131-
132-
*minshaft*: scalar
145+
minshaft : scalar, optional
133146
Length below which arrow scales, in units of head length. Do not
134147
set this to less than 1, or small arrows will look terrible!
135148
Default is 1
136-
137-
*minlength*: scalar
149+
minlength : scalar, optional
138150
Minimum length as a multiple of shaft width; if an arrow length
139151
is less than this, plot a dot (hexagon) of this diameter instead.
140152
Default is 1.
141-
142-
*pivot*: [ 'tail' | 'mid' | 'middle' | 'tip' ]
153+
pivot : [ 'tail' | 'mid' | 'middle' | 'tip' ], optional
143154
The part of the arrow that is at the grid point; the arrow rotates
144155
about this point, hence the name *pivot*.
145-
146-
*color*: [ color | color sequence ]
156+
color : [ color | color sequence ], optional
147157
This is a synonym for the
148158
:class:`~matplotlib.collections.PolyCollection` facecolor kwarg.
149159
If *C* has been set, *color* has no effect.
150160
151-
The defaults give a slightly swept-back arrow; to make the head a
152-
triangle, make *headaxislength* the same as *headlength*. To make the
153-
arrow more pointed, reduce *headwidth* or increase *headlength* and
154-
*headaxislength*. To make the head smaller relative to the shaft,
155-
scale down all the head parameters. You will probably do best to leave
156-
minshaft alone.
157-
158-
linewidths and edgecolors can be used to customize the arrow
159-
outlines. Additional :class:`~matplotlib.collections.PolyCollection`
161+
Notes
162+
-----
163+
Additional :class:`~matplotlib.collections.PolyCollection`
160164
keyword arguments:
161165
162166
%(PolyCollection)s
167+
168+
Examples
169+
--------
170+
.. plot:: mpl_examples/pylab_examples/quiver_simple_demo.py
171+
172+
See Also
173+
--------
174+
quiverkey : Add a key to a quiver plot
163175
""" % docstring.interpd.params
164176

165177
_quiverkey_doc = """

0 commit comments

Comments
 (0)