|
8 | 8 | desired. In particular, note the usage of the "origin" and "extent"
|
9 | 9 | keyword arguments to imshow and contour.
|
10 | 10 | '''
|
11 |
| -from pylab import * |
| 11 | +import matplotlib.pyplot as plt |
| 12 | +import numpy as np |
| 13 | +from matplotlib import mlab, cm |
12 | 14 |
|
13 | 15 | # Default delta is large because that makes it fast, and it illustrates
|
14 | 16 | # the correct registration between image and contours.
|
15 | 17 | delta = 0.5
|
16 | 18 |
|
17 | 19 | extent = (-3, 4, -4, 3)
|
18 | 20 |
|
19 |
| -x = arange(-3.0, 4.001, delta) |
20 |
| -y = arange(-4.0, 3.001, delta) |
21 |
| -X, Y = meshgrid(x, y) |
22 |
| -Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) |
23 |
| -Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) |
| 21 | +x = np.arange(-3.0, 4.001, delta) |
| 22 | +y = np.arange(-4.0, 3.001, delta) |
| 23 | +X, Y = np.meshgrid(x, y) |
| 24 | +Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) |
| 25 | +Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) |
24 | 26 | Z = (Z1 - Z2) * 10
|
25 | 27 |
|
26 |
| -levels = arange(-2.0, 1.601, 0.4) # Boost the upper limit to avoid truncation errors. |
| 28 | +levels = np.arange(-2.0, 1.601, 0.4) # Boost the upper limit to avoid truncation errors. |
27 | 29 |
|
28 | 30 | norm = cm.colors.Normalize(vmax=abs(Z).max(), vmin=-abs(Z).max())
|
29 | 31 | cmap = cm.PRGn
|
30 | 32 |
|
31 |
| -figure() |
| 33 | +plt.figure() |
32 | 34 |
|
33 | 35 |
|
34 |
| -subplot(2, 2, 1) |
| 36 | +plt.subplot(2, 2, 1) |
35 | 37 |
|
36 |
| -cset1 = contourf(X, Y, Z, levels, |
| 38 | +cset1 = plt.contourf(X, Y, Z, levels, |
37 | 39 | cmap=cm.get_cmap(cmap, len(levels) - 1),
|
38 | 40 | norm=norm,
|
39 | 41 | )
|
|
45 | 47 | # contour separately; don't try to change the edgecolor or edgewidth
|
46 | 48 | # of the polygons in the collections returned by contourf.
|
47 | 49 | # Use levels output from previous call to guarantee they are the same.
|
48 |
| -cset2 = contour(X, Y, Z, cset1
F438
span>.levels, |
| 50 | +cset2 = plt.contour(X, Y, Z, cset1.levels, |
49 | 51 | colors='k',
|
50 | 52 | hold='on')
|
51 | 53 | # We don't really need dashed contour lines to indicate negative
|
|
57 | 59 | # to set up an array of colors and linewidths.
|
58 | 60 | # We are making a thick green line as a zero contour.
|
59 | 61 | # Specify the zero level as a tuple with only 0 in it.
|
60 |
| -cset3 = contour(X, Y, Z, (0,), |
| 62 | +cset3 = plt.contour(X, Y, Z, (0,), |
61 | 63 | colors='g',
|
62 | 64 | linewidths=2,
|
63 | 65 | hold='on')
|
64 |
| -title('Filled contours') |
65 |
| -colorbar(cset1) |
| 66 | +plt.title('Filled contours') |
| 67 | +plt.colorbar(cset1) |
66 | 68 | #hot()
|
67 | 69 |
|
68 | 70 |
|
69 |
| -subplot(2, 2, 2) |
| 71 | +plt.subplot(2, 2, 2) |
70 | 72 |
|
71 |
| -imshow(Z, extent=extent, cmap=cmap, norm=norm) |
72 |
| -v = axis() |
73 |
| -contour(Z, levels, hold='on', colors='k', |
| 73 | +plt.imshow(Z, extent=extent, cmap=cmap, norm=norm) |
| 74 | +v = plt.axis() |
| 75 | +plt.contour(Z, levels, hold='on', colors='k', |
74 | 76 | origin='upper', extent=extent)
|
75 |
| -axis(v) |
76 |
| -title("Image, origin 'upper'") |
| 77 | +plt.axis(v) |
| 78 | +plt.title("Image, origin 'upper'") |
77 | 79 |
|
78 |
| -subplot(2, 2, 3) |
| 80 | +plt.subplot(2, 2, 3) |
79 | 81 |
|
80 |
| -imshow(Z, origin='lower', extent=extent, cmap=cmap, norm=norm) |
81 |
| -v = axis() |
82 |
| -contour(Z, levels, hold='on', colors='k', |
| 82 | +plt.imshow(Z, origin='lower', extent=extent, cmap=cmap, norm=norm) |
| 83 | +v = plt.axis() |
| 84 | +plt.contour(Z, levels, hold='on', colors='k', |
83 | 85 | origin='lower', extent=extent)
|
84 |
| -axis(v) |
85 |
| -title("Image, origin 'lower'") |
| 86 | +plt.axis(v) |
| 87 | +plt.title("Image, origin 'lower'") |
86 | 88 |
|
87 |
| -subplot(2, 2, 4) |
| 89 | +plt.subplot(2, 2, 4) |
88 | 90 |
|
89 | 91 | # We will use the interpolation "nearest" here to show the actual
|
90 | 92 | # image pixels.
|
91 | 93 | # Note that the contour lines don't extend to the edge of the box.
|
92 | 94 | # This is intentional. The Z values are defined at the center of each
|
93 | 95 | # image pixel (each color block on the following subplot), so the
|
94 | 96 | # domain that is contoured does not extend beyond these pixel centers.
|
95 |
| -im = imshow(Z, interpolation='nearest', extent=extent, cmap=cmap, norm=norm) |
96 |
| -v = axis() |
97 |
| -contour(Z, levels, hold='on', colors='k', |
| 97 | +im = plt.imshow(Z, interpolation='nearest', extent=extent, cmap=cmap, norm=norm) |
| 98 | +v = plt.axis() |
| 99 | +plt.contour(Z, levels, hold='on', colors='k', |
98 | 100 | origin='image', extent=extent)
|
99 |
| -axis(v) |
100 |
| -ylim = get(gca(), 'ylim') |
101 |
| -setp(gca(), ylim=ylim[::-1]) |
102 |
| -title("Image, origin from rc, reversed y-axis") |
103 |
| -colorbar(im) |
| 101 | +plt.axis(v) |
| 102 | +ylim = plt.get(plt.gca(), 'ylim') |
| 103 | +plt.setp(plt.gca(), ylim=ylim[::-1]) |
| 104 | +plt.title("Image, origin from rc, reversed y-axis") |
| 105 | +plt.colorbar(im) |
104 | 106 |
|
105 |
| -show() |
| 107 | +plt.show() |
0 commit comments