|
2 | 2 | ========== |
3 | 3 | Streamplot |
4 | 4 | ========== |
5 | | -A streamplot, or streamline plot, is used to display 2D vector fields. This |
| 5 | +
|
| 6 | +A stream plot, or stream line plot, is used to display 2D vector fields. This |
6 | 7 | example shows a few features of the stream plot function: |
7 | 8 |
|
8 | | - * Varying the color along a streamline. |
9 | | - * Varying the density of streamlines. |
| 9 | + * Varying the color along a stream line. |
| 10 | + * Varying the density of stream lines. |
10 | 11 | * Varying the line width along a stream line. |
11 | | - * Controlling the start points of streamlines. |
12 | | - * Streamlines skipping masked regions and NaN values. |
| 12 | + * Controlling the starting points of stream lines. |
| 13 | + * Stream lines skipping masked regions and NaN values. |
13 | 14 | """ |
14 | 15 | import numpy as np |
15 | 16 | import matplotlib.pyplot as plt |
| 17 | +import matplotlib.gridspec as gridspec |
16 | 18 |
|
17 | 19 | w = 3 |
18 | 20 | Y, X = np.mgrid[-w:w:100j, -w:w:100j] |
19 | 21 | U = -1 - X**2 + Y |
20 | 22 | V = 1 + X - Y**2 |
21 | 23 | speed = np.sqrt(U*U + V*V) |
22 | 24 |
|
23 | | -fig = plt.figure() |
| 25 | +fig = plt.figure(figsize=(7, 9)) |
| 26 | +gs = gridspec.GridSpec(nrows=3, ncols=2, height_ratios=[1, 1, 2]) |
24 | 27 |
|
25 | 28 | # Varying density along a streamline |
26 | | -ax0 = fig.add_subplot(321) |
| 29 | +ax0 = fig.add_subplot(gs[0, 0]) |
27 | 30 | ax0.streamplot(X, Y, U, V, density=[0.5, 1]) |
28 | 31 | ax0.set_title('Varying Density') |
29 | 32 |
|
30 | 33 | # Varying color along a streamline |
31 | | -ax1 = fig.add_subplot(322) |
32 | | -strm = ax1.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=plt.cm.autumn) |
| 34 | +ax1 = fig.add_subplot(gs[0, 1]) |
| 35 | +strm = ax1.streamplot(X, Y, U, V, color=U, linewidth=2, cmap='autumn') |
33 | 36 | fig.colorbar(strm.lines) |
34 | 37 | ax1.set_title('Varying color') |
35 | 38 |
|
36 | 39 | # Varying line width along a streamline |
37 | | -ax2 = fig.add_subplot(323) |
| 40 | +ax2 = fig.add_subplot(gs[1, 0]) |
38 | 41 | lw = 5*speed / speed.max() |
39 | 42 | ax2.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw) |
40 | 43 | ax2.set_title('Varying Line Width') |
|
45 | 48 | U, V = np.mgrid[-3:3:100j, 0:0:100j] |
46 | 49 | seed_points = np.array([[-2, -1, 0, 1, 2, -1], [-2, -1, 0, 1, 2, 2]]) |
47 | 50 |
|
48 | | -ax3 = fig.add_subplot(324) |
| 51 | +ax3 = fig.add_subplot(gs[1, 1]) |
49 | 52 | strm = ax3.streamplot(X, Y, U, V, color=U, linewidth=2, |
50 | | - cmap=plt.cm.autumn, start_points=seed_points.T) |
| 53 | + cmap='autumn', start_points=seed_points.T) |
51 | 54 | fig.colorbar(strm.lines) |
52 | 55 | ax3.set_title('Controlling Starting Points') |
53 | 56 |
|
54 | | -# Displaying the starting points with red symbols. |
| 57 | +# Displaying the starting points with blue symbols. |
55 | 58 | ax3.plot(seed_points[0], seed_points[1], 'bo') |
56 | | - |
57 | | -ax3.axis((-3, 3, -3, 3)) |
| 59 | +ax3.axis((-w, w, -w, w)) |
58 | 60 |
|
59 | 61 | # Create a mask |
60 | 62 | w = 3 |
|
68 | 70 | U[:20, :20] = np.nan |
69 | 71 | U = np.ma.array(U, mask=mask) |
70 | 72 |
|
71 | | -ax4 = fig.add_subplot(325) |
| 73 | +ax4 = fig.add_subplot(gs[2:, :]) |
72 | 74 | ax4.streamplot(X, Y, U, V, color='r') |
73 | | -ax4.set_title('Streamline with Masking') |
| 75 | +ax4.set_title('Streamplot with Masking') |
74 | 76 |
|
75 | 77 | ax4.imshow(~mask, extent=(-w, w, -w, w), alpha=0.5, |
76 | | - interpolation='nearest', cmap=plt.cm.gray, aspect='auto') |
| 78 | + interpolation='nearest', cmap='gray', aspect='auto') |
| 79 | +ax4.set_aspect('equal') |
77 | 80 |
|
78 | 81 | plt.tight_layout() |
79 | 82 | plt.show() |
0 commit comments