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

Skip to content

Commit a7bfef2

Browse files
committed
mep12 on quiver_demo.py
1 parent 86528e7 commit a7bfef2

File tree

1 file changed

+83
-58
lines changed

1 file changed

+83
-58
lines changed

examples/pylab_examples/quiver_demo.py

Lines changed: 83 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,101 @@
88
The workaround is to manually expand the axes.
99
1010
'''
11-
from pylab import *
11+
import matplotlib.pyplot as plt
12+
import numpy as np
1213
from numpy import ma
1314

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)
1718

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()
2425
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])
2627

27-
title('Minimal arguments, no kwargs')
28+
plt.title('Minimal arguments, no kwargs')
2829

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')
3839

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(
45+
Q,
46+
0.5,
47+
0.03,
48+
1,
49+
r'$1 \frac{m}{s}$',
50+
fontproperties={
51+
'weight': 'bold'})
52+
plt.plot(X[::3, ::3], Y[::3, ::3], 'k.')
53+
plt.axis([-1, 7, -1, 7])
54+
plt.title("pivot='mid'; every third arrow; units='inches'")
4755

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'")
56+
# 4
57+
plt.figure()
58+
M = np.sqrt(np.power(U, 2) + np.power(V, 2))
59+
Q = plt.quiver(
60+
X,
61+
Y,
62+
U,
63+
V,
64+
M,
65+
units='x',
66+
pivot='tip',
67+
width=0.022,
68+
scale=1 /
69+
0.15)
70+
qk = plt.quiverkey(Q, 0.9, 1.05, 1, r'$1 \frac{m}{s}$',
71+
labelpos='E',
72+
fontproperties={'weight': 'bold'})
73+
plt.plot(X, Y, 'k.')
74+
plt.axis([-1, 7, -1, 7])
75+
plt.title("scales with x view; pivot='tip'")
5876

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")
77+
# 5
78+
plt.figure()
79+
Q = plt.quiver(X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3],
80+
color='r', units='x',
81+
linewidths=(2,), edgecolors=('k'), headaxislength=5)
82+
qk = plt.quiverkey(
83+
Q,
84+
0.5,
85+
0.03,
86+
1,
87+
r'$1 \frac{m}{s}$',
88+
fontproperties={
89+
'weight': 'bold'})
90+
plt.axis([-1, 7, -1, 7])
91+
plt.title("triangular head; scale with x view; black edges")
6792

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
93+
# 6
94+
plt.figure()
95+
M = np.zeros(U.shape, dtype='bool')
96+
M[U.shape[0] / 3:2 * U.shape[0] / 3, U.shape[1] / 3:2 * U.shape[1] / 3] = True
7297
U = ma.masked_array(U, mask=M)
7398
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()
99+
Q = plt.quiver(U, V)
100+
qk = plt.quiverkey(Q, 0.5, 0.92, 2, r'$2 \frac{m}{s}$', labelpos='W',
101+
fontproperties={'weight': 'bold'})
102+
l, r, b, t = plt.axis()
78103
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')
104+
plt.axis([l - 0.05 * dx, r + 0.05 * dx, b - 0.05 * dy, t + 0.05 * dy])
105+
plt.title('Minimal arguments, no kwargs - masked values')
81106

82107

83-
show()
108+
plt.show()

0 commit comments

Comments
 (0)