|
| 1 | +#!/usr/bin/env python |
| 2 | +''' |
| 3 | +contour_image.py [options] |
| 4 | +
|
| 5 | +Test combinations of contouring, filled contouring, and image plotting. |
| 6 | +''' |
| 7 | +from pylab import * |
| 8 | +import matplotlib.numerix |
| 9 | +print 'Using', matplotlib.numerix.which[0] |
| 10 | + |
| 11 | +from optparse import OptionParser |
| 12 | +parser = OptionParser(usage=__doc__.rstrip()) |
| 13 | +parser.add_option("-b", "--badmask", dest="badmask", default="none", |
| 14 | + help="'none', 'edge', 'interior'; default is 'none'") |
| 15 | +parser.add_option("-d", "--delta", dest="delta", type="float", default=0.5, |
| 16 | + help="grid increment in x and y; default is 0.5") |
| 17 | +parser.add_option("-s", "--save", dest="save", default=None, metavar="FILE", |
| 18 | + help="Save to FILE; default is to not save.") |
| 19 | + |
| 20 | +#Default delta is large because that makes it fast, and it illustrates |
| 21 | +# the correct registration between image and contours. |
| 22 | + |
| 23 | +import sys |
| 24 | +# We have to strip out the numerix arguments before passing the |
| 25 | +# input arguments to the parser. |
| 26 | +Args = [arg for arg in sys.argv if arg not in ('--Numeric', '--numarray')] |
| 27 | +options, args = parser.parse_args(Args) |
| 28 | +delta = options.delta |
| 29 | +badmask = options.badmask |
| 30 | + |
| 31 | +x = y = arange(-3.0, 3.01, delta) |
| 32 | +X, Y = meshgrid(x, y) |
| 33 | +Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) |
| 34 | +Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) |
| 35 | +Z = (Z1 - Z2) * 10 |
| 36 | +Zbm = Z.copy() |
| 37 | + |
| 38 | +ny, nx = shape(Z) |
| 39 | + |
| 40 | +Badmask = zeros(shape(Z)) |
| 41 | +if badmask == 'none': |
| 42 | + Badmask = None |
| 43 | +elif badmask == 'edge': |
| 44 | + Badmask[:ny/5,:] = 1 |
| 45 | + Zbm[:ny/5,:] = 100 # test ability to ignore bad values |
| 46 | +elif badmask == 'interior': |
| 47 | + Badmask[ny/4:ny/2, nx/4:nx/2] = 1 |
| 48 | + Zbm[ny/4:ny/2, nx/4:nx/2] = 100 |
| 49 | + print "Interior masking works correctly for line contours only..." |
| 50 | +else: |
| 51 | + raise ValueError("badmask must be 'none', 'edge', or 'interior'") |
| 52 | + |
| 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") |
| 95 | + |
| 96 | +if options.save is not None: |
| 97 | + savefig(options.save) |
| 98 | + |
| 99 | +show() |
| 100 | + |
0 commit comments