|
1 | 1 | #!/usr/bin/env python |
2 | 2 | ''' |
3 | | -contour_image.py [options] |
4 | | -
|
5 | 3 | Test combinations of contouring, filled contouring, and image plotting. |
6 | 4 | For contour labelling, see contour_demo.py. |
| 5 | +
|
| 6 | +The emphasis in this demo is on showing how to make contours register |
| 7 | +correctly on images, and on how to get both of them oriented as |
| 8 | +desired. In particular, note the usage of the "origin" and "extent" |
| 9 | +keyword arguments to imshow and contour. |
7 | 10 | ''' |
8 | 11 | from pylab import * |
9 | | -import matplotlib.numerix |
10 | | -print 'Using', matplotlib.numerix.which[0] |
11 | | - |
12 | | -from optparse import OptionParser |
13 | | -parser = OptionParser(usage=__doc__.rstrip()) |
14 | | -parser.add_option("-b", "--badmask", dest="badmask", default="none", |
15 | | - help="'none', 'edge', 'interior'; default is 'none'") |
16 | | -parser.add_option("-d", "--delta", dest="delta", type="float", default=0.5, |
17 | | - help="grid increment in x and y; default is 0.5") |
18 | | -parser.add_option("-s", "--save", dest="save", default=None, metavar="FILE", |
19 | | - help="Save to FILE; default is to not save.") |
20 | | -parser.add_option("-e", "--extent", dest = "extent", type="int", default=0, |
21 | | - help="""For subplots 2-4, use extent: \ |
22 | | -specify number 1 through 4 for any of 4 possibilities.""") |
23 | | -parser.add_option("-f", "--figure", dest = "fignum", type="int", default=0, |
24 | | - metavar="FIGNUM", |
25 | | - help="""Plot subplot FIGNUM as a full-size plot; FIGNUM \ |
26 | | -must be in the range 1-4.""") |
27 | 12 |
|
28 | 13 | #Default delta is large because that makes it fast, and it illustrates |
29 | 14 | # the correct registration between image and contours. |
| 15 | +delta = 0.5 |
| 16 | + |
| 17 | +extent = (-3,4,-4,3) |
30 | 18 |
|
31 | | -import sys |
32 | | -# We have to strip out the numerix arguments before passing the |
33 | | -# input arguments to the parser. |
34 | | -Args = [arg for arg in sys.argv if arg not in ('--Numeric', '--numarray')] |
35 | | -options, args = parser.parse_args(Args) |
36 | | -delta = options.delta |
37 | | -badmask = options.badmask |
38 | | - |
39 | | -extents = ((-3,4,-4,3), (-3,4,3,-4), (4,-3,-4,3), (4,-3,3,-4)) |
40 | | -if options.extent == 0: |
41 | | - extent = None |
42 | | -elif options.extent <= 4 and options.extent > 0: |
43 | | - extent = extents[options.extent - 1] |
44 | | - print "Using extent ", extent, "to change axis mapping on subplots 2-4" |
45 | | -else: |
46 | | - raise ValueError("extent must be integer, 1-4") |
47 | | - |
48 | | -fignum = options.fignum |
49 | | - |
50 | | -x = y = arange(-3.0, 3.01, delta) |
| 19 | +x = arange(-3.0, 4.001, delta) |
| 20 | +y = arange(-4.0, 3.001, delta) |
51 | 21 | X, Y = meshgrid(x, y) |
52 | 22 | Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) |
53 | 23 | Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) |
54 | 24 | Z = (Z1 - Z2) * 10 |
55 | | -Zbm = Z.copy() |
56 | | - |
57 | | -ny, nx = shape(Z) |
58 | | - |
59 | | -Badmask = zeros(shape(Z)) |
60 | | -if badmask == 'none': |
61 | | - Badmask = None |
62 | | -elif badmask == 'edge': |
63 | | - Badmask[:ny/5,:] = 1 |
64 | | - Zbm[:ny/5,:] = 100 # test ability to ignore bad values |
65 | | -elif badmask == 'interior': |
66 | | - Badmask[ny/4:ny/2, nx/4:nx/2] = 1 |
67 | | - Zbm[ny/4:ny/2, nx/4:nx/2] = 100 |
68 | | - print "Interior masking works correctly for line contours only..." |
69 | | -else: |
70 | | - raise ValueError("badmask must be 'none', 'edge', or 'interior'") |
71 | 25 |
|
72 | | -levels = arange(-1.2,1.5,0.4) |
| 26 | +levels = arange(-2.0,1.6,0.4) |
73 | 27 |
|
74 | 28 | figure() |
75 | 29 |
|
76 | 30 |
|
77 | | -if fignum == 0: |
78 | | - subplot(2,2,1) |
79 | | - |
80 | | -if fignum == 0 or fignum == 1: |
81 | | - levs1, colls = contourf(X, Y, Zbm, 10, |
82 | | - cmap=cm.jet, |
83 | | - badmask = Badmask |
84 | | - ) |
85 | | - #If we want lines as well as filled regions, we need to call |
86 | | - # contour separately; don't try to change the edgecolor or edgewidth |
87 | | - # of the polygons in the collections returned by contourf. |
88 | | - # Use levels output from previous call to guarantee they are the same. |
89 | | - levs2, colls2 = contour(X, Y, Zbm, levs1, |
90 | | - colors = 'k', |
91 | | - badmask = Badmask, |
92 | | - hold='on') |
93 | | - # We don't really need dashed contour lines to indicate negative |
94 | | - # regions, so let's turn them off. |
95 | | - for c in colls2: |
96 | | - c.set_linestyle('solid') |
97 | | - |
98 | | - # It is easier here to make a separate call to contour than |
99 | | - # to set up an array of colors and linewidths. |
100 | | - levs3, colls3 = contour(X, Y, Zbm, (0,), |
101 | | - colors = 'g', |
102 | | - linewidths = 2, |
103 | | - hold='on') |
104 | | - title('Filled contours') |
105 | | - colorbar() |
106 | | - hot() |
107 | | - # Major reworking of the colorbar mechanism is needed for filled contours! |
108 | | - |
109 | | -if fignum == 0: |
110 | | - subplot(2,2,2) |
111 | | - |
112 | | -if fignum == 0 or fignum == 2: |
113 | | - imshow(Z, extent=extent) |
114 | | - v = axis() |
115 | | - contour(Z, levels, hold='on', colors = 'k', origin='upper', extent=extent) |
116 | | - axis(v) |
117 | | - title("Image, origin 'upper'") |
118 | | - |
119 | | -if fignum == 0: |
120 | | - subplot(2,2,3) |
121 | | - |
122 | | -if fignum == 0 or fignum == 3: |
123 | | - imshow(Z, origin='lower', extent=extent) |
124 | | - v = axis() |
125 | | - contour(Z, levels, hold='on', colors = 'k', origin='lower', extent=extent) |
126 | | - axis(v) |
127 | | - title("Image, origin 'lower'") |
128 | | - |
129 | | -if fignum == 0: |
130 | | - subplot(2,2,4) |
131 | | - |
132 | | -if fignum == 0 or fignum == 4: |
133 | | - imshow(Z, interpolation='nearest', extent=extent) |
134 | | - v = axis() |
135 | | - contour(Z, levels, hold='on', colors = 'k', origin='image', extent=extent) |
136 | | - axis(v) |
137 | | - ylim = get(gca(), 'ylim') |
138 | | - set(gca(), ylim=ylim[::-1]) |
139 | | - title("Image, origin from rc, reversed y-axis") |
140 | | - |
141 | | -if options.save is not None: |
142 | | - savefig(options.save) |
| 31 | +subplot(2,2,1) |
| 32 | + |
| 33 | +levs1, colls = contourf(X, Y, Z, levels, |
| 34 | + cmap=cm.jet, |
| 35 | + ) |
| 36 | +#If we want lines as well as filled regions, we need to call |
| 37 | +# contour separately; don't try to change the edgecolor or edgewidth |
| 38 | +# of the polygons in the collections returned by contourf. |
| 39 | +# Use levels output from previous call to guarantee they are the same. |
| 40 | +levs2, colls2 = contour(X, Y, Z, levs1, |
| 41 | + colors = 'k', |
| 42 | + hold='on') |
| 43 | +# We don't really need dashed contour lines to indicate negative |
| 44 | +# regions, so let's turn them off. |
| 45 | +for c in colls2: |
| 46 | + c.set_linestyle('solid') |
| 47 | + |
| 48 | +# It is easier here to make a separate call to contour than |
| 49 | +# to set up an array of colors and linewidths. |
| 50 | +# We are making a thick green line as a zero contour. |
| 51 | +# Specify the zero level as a tuple with only 0 in it. |
| 52 | +levs3, colls3 = contour(X, Y, Z, (0,), |
| 53 | + colors = 'g', |
| 54 | + linewidths = 2, |
| 55 | + hold='on') |
| 56 | +title('Filled contours') |
| 57 | +#colorbar() |
| 58 | +hot() |
| 59 | +# To Do: make a discrete colorbar to match filled contours. |
| 60 | + |
| 61 | +subplot(2,2,2) |
| 62 | + |
| 63 | +imshow(Z, extent=extent) |
| 64 | +v = axis() |
| 65 | +contour(Z, levels, hold='on', colors = 'k', origin='upper', extent=extent) |
| 66 | +axis(v) |
| 67 | +title("Image, origin 'upper'") |
| 68 | + |
| 69 | +subplot(2,2,3) |
| 70 | + |
| 71 | +imshow(Z, origin='lower', extent=extent) |
| 72 | +v = axis() |
| 73 | +contour(Z, levels, hold='on', colors = 'k', origin='lower', extent=extent) |
| 74 | +axis(v) |
| 75 | +title("Image, origin 'lower'") |
| 76 | + |
| 77 | +subplot(2,2,4) |
| 78 | + |
| 79 | +# We will use the interpolation "nearest" here to show the actual |
| 80 | +# image pixels. |
| 81 | +# Note that the contour lines don't extend to the edge of the box. |
| 82 | +# This is intentional. The Z values are defined at the center of each |
| 83 | +# image pixel (each color block on the following subplot), so the |
| 84 | +# domain that is contoured does not extend beyond these pixel centers. |
| 85 | +imshow(Z, interpolation='nearest', extent=extent) |
| 86 | +v = axis() |
| 87 | +contour(Z, levels, hold='on', colors = 'k', origin='image', extent=extent) |
| 88 | +axis(v) |
| 89 | +ylim = get(gca(), 'ylim') |
| 90 | +set(gca(), ylim=ylim[::-1]) |
| 91 | +title("Image, origin from rc, reversed y-axis") |
| 92 | + |
| 93 | +#savefig('contour_image') |
143 | 94 |
|
144 | 95 | show() |
0 commit comments