|
8 | 8 | The workaround is to manually expand the axes.
|
9 | 9 |
|
10 | 10 | '''
|
11 |
| -from pylab import * |
| 11 | +import matplotlib.pyplot as plt |
| 12 | +import numpy as np |
12 | 13 | from numpy import ma
|
13 | 14 |
|
14 |
| -X, Y = meshgrid(arange(0, 2*pi, .2), arange(0, 2*pi, .2)) |
15 |
| -U = cos(X) |
16 |
| -V = sin(Y) |
| 15 | +X, Y = np.meshgrid(np.arange(0, 2 * np.pi, .2), np.arange(0, 2 * np.pi, .2)) |
| 16 | +U = np.cos(X) |
| 17 | +V = np.sin(Y) |
17 | 18 |
|
18 |
| -#1 |
19 |
| -figure() |
20 |
| -Q = quiver(U, V) |
21 |
| -qk = quiverkey(Q, 0.5, 0.92, 2, r'$2 \frac{m}{s}$', labelpos='W', |
22 |
| - fontproperties={'weight': 'bold'}) |
23 |
| -l, r, b, t = axis() |
| 19 | +# 1 |
| 20 | +plt.figure() |
| 21 | +Q = plt.quiver(U, V) |
| 22 | +qk = plt.quiverkey(Q, 0.5, 0.92, 2, r'$2 \frac{m}{s}$', labelpos='W', |
| 23 | + fontproperties={'weight': 'bold'}) |
| 24 | +l, r, b, t = plt.axis() |
24 | 25 | dx, dy = r - l, t - b
|
25 |
| -axis([l - 0.05*dx, r + 0.05*dx, b - 0.05*dy, t + 0.05*dy]) |
| 26 | +plt.axis([l - 0.05*dx, r + 0.05*dx, b - 0.05*dy, t + 0.05*dy]) |
26 | 27 |
|
27 |
| -title('Minimal arguments, no kwargs') |
| 28 | +plt.title('Minimal arguments, no kwargs') |
28 | 29 |
|
29 |
| -#2 |
30 |
| -figure() |
31 |
| -Q = quiver(X, Y, U, V, units='width') |
32 |
| -qk = quiverkey(Q, 0.9, 0.95, 2, r'$2 \frac{m}{s}$', |
33 |
| - labelpos='E', |
34 |
| - coordinates='figure', |
35 |
| - fontproperties={'weight': 'bold'}) |
36 |
| -axis([-1, 7, -1, 7]) |
37 |
| -title('scales with plot width, not view') |
| 30 | +# 2 |
| 31 | +plt.figure() |
| 32 | +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') |
38 | 39 |
|
39 |
| -#3 |
40 |
| -figure() |
41 |
| -Q = quiver(X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3], |
42 |
| - pivot='mid', color='r', units='inches') |
43 |
| -qk = quiverkey(Q, 0.5, 0.03, 1, r'$1 \frac{m}{s}$', fontproperties={'weight': 'bold'}) |
44 |
| -plot(X[::3, ::3], Y[::3, ::3], 'k.') |
45 |
| -axis([-1, 7, -1, 7]) |
46 |
| -title("pivot='mid'; every third arrow; units='inches'") |
| 40 | +# 3 |
| 41 | +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]) |
| 48 | +plt.title("pivot='mid'; every third arrow; units='inches'") |
47 | 49 |
|
48 |
| -#4 |
49 |
| -figure() |
50 |
| -M = sqrt(pow(U, 2) + pow(V, 2)) |
51 |
| -Q = quiver(X, Y, U, V, M, units='x', pivot='tip', width=0.022, scale=1/0.15) |
52 |
| -qk = quiverkey(Q, 0.9, 1.05, 1, r'$1 \frac{m}{s}$', |
53 |
| - labelpos='E', |
54 |
| - fontproperties={'weight': 'bold'}) |
55 |
| -plot(X, Y, 'k.') |
56 |
| -axis([-1, 7, -1, 7]) |
57 |
| -title("scales with x view; pivot='tip'") |
| 50 | +# 4 |
| 51 | +plt.figure() |
| 52 | +M = np.hypot(U, V) |
| 53 | +Q = plt.quiver(X, Y, U, V, M, |
| 54 | + units='x', |
| 55 | + pivot='tip', |
| 56 | + width=0.022, |
| 57 | + 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.') |
| 62 | +plt.axis([-1, 7, -1, 7]) |
| 63 | +plt.title("scales with x view; pivot='tip'") |
58 | 64 |
|
59 |
| -#5 |
60 |
| -figure() |
61 |
| -Q = quiver(X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3], |
62 |
| - color='r', units='x', |
63 |
| - linewidths=(2,), edgecolors=('k'), headaxislength=5) |
64 |
| -qk = quiverkey(Q, 0.5, 0.03, 1, r'$1 \frac{m}{s}$', fontproperties={'weight': 'bold'}) |
65 |
| -axis([-1, 7, -1, 7]) |
66 |
| -title("triangular head; scale with x view; black edges") |
| 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=(2,), 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") |
67 | 74 |
|
68 |
| -#6 |
69 |
| -figure() |
70 |
| -M = zeros(U.shape, dtype='bool') |
71 |
| -M[U.shape[0]/3:2*U.shape[0]/3, U.shape[1]/3:2*U.shape[1]/3] = True |
| 75 | +# 6 |
| 76 | +plt.figure() |
| 77 | +M = np.zeros(U.shape, dtype='bool') |
| 78 | +M[U.shape[0]/3:2*U.shape[0]/3, |
| 79 | + U.shape[1]/3:2*U.shape[1]/3] = True |
72 | 80 | U = ma.masked_array(U, mask=M)
|
73 | 81 | V = ma.masked_array(V, mask=M)
|
74 |
| -Q = quiver(U, V) |
75 |
| -qk = quiverkey(Q, 0.5, 0.92, 2, r'$2 \frac{m}{s}$', labelpos='W', |
76 |
| - fontproperties={'weight': 'bold'}) |
77 |
| -l, r, b, t = axis() |
| 82 | +Q = plt.quiver(U, V) |
| 83 | +qk = plt.quiverkey(Q, 0.5, 0.92, 2, r'$2 \frac{m}{s}$', labelpos='W', |
| 84 | + fontproperties={'weight': 'bold'}) |
| 85 | +l, r, b, t = plt.axis() |
78 | 86 | dx, dy = r - l, t - b
|
79 |
| -axis([l - 0.05*dx, r + 0.05*dx, b - 0.05*dy, t + 0.05*dy]) |
80 |
| -title('Minimal arguments, no kwargs - masked values') |
| 87 | +plt.axis([l - 0.05 * dx, r + 0.05 * dx, b - 0.05 * dy, t + 0.05 * dy]) |
| 88 | +plt.title('Minimal arguments, no kwargs - masked values') |
81 | 89 |
|
82 | 90 |
|
83 |
| -show() |
| 91 | +plt.show() |
0 commit comments