|
1 | 1 | #!/usr/bin/env python
|
2 |
| -from pylab import * |
| 2 | +import numpy as np |
| 3 | +import matplotlib.pyplot as plt |
| 4 | + |
3 | 5 | origin = 'lower'
|
4 | 6 | #origin = 'upper'
|
5 | 7 |
|
6 | 8 | delta = 0.025
|
7 | 9 |
|
8 |
| -x = y = arange(-3.0, 3.01, delta) |
9 |
| -X, Y = meshgrid(x, y) |
10 |
| -Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) |
11 |
| -Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) |
| 10 | +x = y = np.arange(-3.0, 3.01, delta) |
| 11 | +X, Y = np.meshgrid(x, y) |
| 12 | +Z1 = plt.mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) |
| 13 | +Z2 = plt.mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) |
12 | 14 | Z = 10 * (Z1 - Z2)
|
13 | 15 |
|
14 | 16 | nr, nc = Z.shape
|
15 | 17 |
|
16 | 18 | # put NaNs in one corner:
|
17 |
| -Z[-nr//6:, -nc//6:] = nan |
| 19 | +Z[-nr//6:, -nc//6:] = np.nan |
18 | 20 | # contourf will convert these to masked
|
19 | 21 |
|
20 | 22 |
|
21 |
| -Z = ma.array(Z) |
| 23 | +Z = np.ma.array(Z) |
22 | 24 | # mask another corner:
|
23 |
| -Z[:nr//6, :nc//6] = ma.masked |
| 25 | +Z[:nr//6, :nc//6] = np.ma.masked |
24 | 26 |
|
25 | 27 | # mask a circle in the middle:
|
26 |
| -interior = sqrt((X**2) + (Y**2)) < 0.5 |
27 |
| -Z[interior] = ma.masked |
| 28 | +interior = np.sqrt((X**2) + (Y**2)) < 0.5 |
| 29 | +Z[interior] = np.ma.masked |
28 | 30 |
|
29 | 31 |
|
30 | 32 | # We are using automatic selection of contour levels;
|
31 | 33 | # this is usually not such a good idea, because they don't
|
32 | 34 | # occur on nice boundaries, but we do it here for purposes
|
33 | 35 | # of illustration.
|
34 |
| -CS = contourf(X, Y, Z, 10, # [-1, -0.1, 0, 0.1], |
| 36 | +CS = plt.contourf(X, Y, Z, 10, # [-1, -0.1, 0, 0.1], |
35 | 37 | #alpha=0.5,
|
36 |
| - cmap=cm.bone, |
| 38 | + cmap=plt.cm.bone, |
37 | 39 | origin=origin)
|
38 | 40 |
|
39 | 41 | # Note that in the following, we explicitly pass in a subset of
|
40 | 42 | # the contour levels used for the filled contours. Alternatively,
|
41 | 43 | # We could pass in additional levels to provide extra resolution,
|
42 | 44 | # or leave out the levels kwarg to use all of the original levels.
|
43 | 45 |
|
44 |
| -CS2 = contour(CS, levels=CS.levels[::2], |
| 46 | +CS2 = plt.contour(CS, levels=CS.levels[::2], |
45 | 47 | colors = 'r',
|
46 | 48 | origin=origin,
|
47 | 49 | hold='on')
|
48 | 50 |
|
49 |
| -title('Nonsense (3 masked regions)') |
50 |
| -xlabel('word length anomaly') |
51 |
| -ylabel('sentence length anomaly') |
| 51 | +plt.title('Nonsense (3 masked regions)') |
| 52 | +plt.xlabel('word length anomaly') |
| 53 | +plt.ylabel('sentence length anomaly') |
52 | 54 |
|
53 | 55 | # Make a colorbar for the ContourSet returned by the contourf call.
|
54 |
| -cbar = colorbar(CS) |
| 56 | +cbar = plt.colorbar(CS) |
55 | 57 | cbar.ax.set_ylabel('verbosity coefficient')
|
56 | 58 | # Add the contour line levels to the colorbar
|
57 | 59 | cbar.add_lines(CS2)
|
58 | 60 |
|
59 |
| -figure() |
| 61 | +plt.figure() |
60 | 62 |
|
61 | 63 | # Now make a contour plot with the levels specified,
|
62 | 64 | # and with the colormap generated automatically from a list
|
63 | 65 | # of colors.
|
64 | 66 | levels = [-1.5, -1, -0.5, 0, 0.5, 1]
|
65 |
| -CS3 = contourf(X, Y, Z, levels, |
| 67 | +CS3 = plt.contourf(X, Y, Z, levels, |
66 | 68 | colors = ('r', 'g', 'b'),
|
67 | 69 | origin=origin,
|
68 | 70 | extend='both')
|
|
72 | 74 | CS3.cmap.set_under('yellow')
|
73 | 75 | CS3.cmap.set_over('cyan')
|
74 | 76 |
|
75 |
| -CS4 = contour(X, Y, Z, levels, |
| 77 | +CS4 = plt.contour(X, Y, Z, levels, |
76 | 78 | colors = ('k',),
|
77 | 79 | linewidths = (3,),
|
78 | 80 | origin = origin)
|
79 |
| -title('Listed colors (3 masked regions)') |
80 |
| -clabel(CS4, fmt = '%2.1f', colors = 'w', fontsize=14) |
| 81 | +plt.title('Listed colors (3 masked regions)') |
| 82 | +plt.clabel(CS4, fmt = '%2.1f', colors = 'w', fontsize=14) |
81 | 83 |
|
82 | 84 | # Notice that the colorbar command gets all the information it
|
83 | 85 | # needs from the ContourSet object, CS3.
|
84 |
| -colorbar(CS3) |
85 |
| - |
86 |
| -show() |
| 86 | +plt.colorbar(CS3) |
| 87 | + |
| 88 | +# Illustrate all 4 possible "extend" settings: |
| 89 | +extends = ["neither", "both", "min", "max"] |
| 90 | +cmap = plt.cm.get_cmap("winter") |
| 91 | +cmap.set_under("magenta") |
| 92 | +cmap.set_over("yellow") |
| 93 | +# Note: contouring simply excludes masked or nan regions, so |
| 94 | +# instead of using the "bad" colormap value for them, it draws |
| 95 | +# nothing at all in them. Therefore the following would have |
| 96 | +# no effect: |
| 97 | +#cmap.set_bad("red") |
| 98 | + |
| 99 | +fig, axs = plt.subplots(2,2) |
| 100 | +for ax, extend in zip(axs.ravel(), extends): |
| 101 | + cs = ax.contourf(X, Y, Z, levels, cmap=cmap, extend=extend, origin=origin) |
| 102 | + fig.colorbar(cs, ax=ax, shrink=0.9) |
| 103 | + ax.set_title("extend = %s" % extend) |
| 104 | + ax.locator_params(nbins=4) |
| 105 | + |
| 106 | +plt.show() |
87 | 107 |
|
0 commit comments