| 
3 | 3 | contour_image.py [options]  | 
4 | 4 | 
  | 
5 | 5 | Test combinations of contouring, filled contouring, and image plotting.  | 
 | 6 | +For contour labelling, see contour_demo.py.  | 
6 | 7 | '''  | 
7 | 8 | from pylab import *  | 
8 | 9 | import matplotlib.numerix  | 
 | 
16 | 17 |                   help="grid increment in x and y; default is 0.5")  | 
17 | 18 | parser.add_option("-s", "--save", dest="save", default=None, metavar="FILE",  | 
18 | 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.""")  | 
19 | 27 | 
 
  | 
20 | 28 | #Default delta is large because that makes it fast, and it illustrates  | 
21 | 29 | # the correct registration between image and contours.  | 
 | 
28 | 36 | delta = options.delta  | 
29 | 37 | badmask = options.badmask  | 
30 | 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 | + | 
31 | 50 | x = y = arange(-3.0, 3.01, delta)  | 
32 | 51 | X, Y = meshgrid(x, y)  | 
33 | 52 | Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)  | 
 | 
50 | 69 | else:  | 
51 | 70 |     raise ValueError("badmask must be 'none', 'edge', or 'interior'")  | 
52 | 71 | 
 
  | 
53 |  | -figure(1)  | 
54 |  | -subplot(2,2,1)  | 
55 |  | -levels, colls = contourf(X, Y, Zbm, 10, # [-1, -0.1, 0, 0.1],  | 
56 |  | -                        cmap=cm.jet,  | 
57 |  | -                        badmask = Badmask  | 
58 |  | -                        )  | 
59 |  | -# Use levels output from previous call to guarantee they are the same.  | 
60 |  | -levs2, colls2 = contour(X, Y, Zbm, levels,  | 
61 |  | -                        colors = 'r',  | 
62 |  | -                        badmask = Badmask,  | 
63 |  | -                        hold='on')  | 
64 |  | - | 
65 |  | -levs3, colls3 = contour(X, Y, Zbm, (0,),  | 
66 |  | -                        colors = 'g',  | 
67 |  | -                        linewidths = 2,  | 
68 |  | -                        hold='on')  | 
69 |  | -title('Filled contours')  | 
70 |  | -#colorbar()  | 
71 |  | -# Major reworking of the colorbar mechanism is needed for filled contours!  | 
72 |  | - | 
73 |  | -subplot(2,2,2)  | 
74 |  | -imshow(Z)  | 
75 |  | -v = axis()  | 
76 |  | -contour(Z, levels, hold='on', colors = 'r', origin='upper')  | 
77 |  | -axis(v)  | 
78 |  | -title("Image, origin 'upper'")  | 
79 |  | - | 
80 |  | -subplot(2,2,3)  | 
81 |  | -imshow(Z, origin='lower')  | 
82 |  | -v = axis()  | 
83 |  | -contour(Z, levels, hold='on', colors = 'r', origin='lower')  | 
84 |  | -axis(v)  | 
85 |  | -title("Image, origin 'lower'")  | 
86 |  | - | 
87 |  | -subplot(2,2,4)  | 
88 |  | -imshow(Z, interpolation='nearest')  | 
89 |  | -v = axis()  | 
90 |  | -contour(Z, levels, hold='on', colors = 'r', origin='image')  | 
91 |  | -axis(v)  | 
92 |  | -ylim = get(gca(), 'ylim')  | 
93 |  | -set(gca(), ylim=ylim[::-1])  | 
94 |  | -title("Image, origin from rc, reversed y-axis")  | 
 | 72 | +levels = arange(-1.2,1.5,0.4)  | 
 | 73 | + | 
 | 74 | +figure()  | 
 | 75 | + | 
 | 76 | + | 
 | 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")  | 
95 | 140 | 
 
  | 
96 | 141 | if options.save is not None:  | 
97 | 142 |     savefig(options.save)  | 
98 | 143 | 
 
  | 
99 | 144 | show()  | 
100 |  | - | 
 | 
0 commit comments