From 9f4dc1d8982edbc7bff42fe53746f327e91d3ec1 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 30 Nov 2012 11:25:59 -0800 Subject: [PATCH] plot_image.py: prefer matplotlib.pyplot over pylab pylab bundles both the pyplot and numpy namespaces, so a careful program will avoid it and use the underlying modules directly. --- src/plot_image/plot_image.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/plot_image/plot_image.py b/src/plot_image/plot_image.py index 7766530..ef8f057 100755 --- a/src/plot_image/plot_image.py +++ b/src/plot_image/plot_image.py @@ -42,7 +42,7 @@ For usage info, run:: ./plot_image.py --help When run with interactive output (i.e. no `-o ...` option), the -interactive figure is displayed using `pylab.show`, which means that +interactive figure is displayed using `pyplot.show`, which means that you'll have to kill `plot_image.py` using ^C or similar [1]_. For other ideas, see the Matplotlib website [2]_. @@ -55,13 +55,15 @@ import optparse import sys import matplotlib +import matplotlib.cm import matplotlib.image import numpy # Depending on your Matplotlib configuration, you may need to adjust -# your backend. Do this before importing pylab or matplotlib.backends. +# your backend. Do this before importing pylab, matplotlib.pyplot, or +# matplotlib.backends. #matplotlib.use('Agg') # select backend that doesn't require X Windows -#matplotlib.use('GTKAgg') # select backend that supports pylab.show() +#matplotlib.use('GTKAgg') # select backend that supports pyplot.show() # Alternatively, you can configure the backend using # ~/.matplotlib/matplotlibrc: @@ -69,7 +71,7 @@ import numpy # backend : Agg # #backend : GTKAgg -import pylab +import matplotlib.pyplot _DOC = __doc__ @@ -93,7 +95,7 @@ def read_data_1d(stream, nx, ny): array([[ 0., 1., 2., 3., 4.], [ 5., 6., 7., 8., 9.]]) """ - X,Y = pylab.meshgrid(range(nx+1), range(ny+1)) + X,Y = numpy.meshgrid(range(nx+1), range(ny+1)) Z = numpy.loadtxt(stream) assert Z.size == nx*ny, 'Z.size = %d != %d = %dx%d' % ( Z.size, nx*ny, nx, ny) @@ -138,14 +140,14 @@ def read_data_3d(stream): dy = Ys[-1] - Ys[-2] Xs = numpy.append(Xs, Xs[-1] + dx) Ys = numpy.append(Ys, Ys[-1] + dy) - X,Y = pylab.meshgrid(Xs, Ys) + X,Y = numpy.meshgrid(Xs, Ys) return (X,Y,Z) def plot(X, Y, Z, full=False, title=None, contours=None, interpolation=None, cmap=None): """Plot Z over the mesh X, Y. - >>> X, Y = pylab.meshgrid(range(6), range(2)) + >>> X, Y = numpy.meshgrid(range(6), range(2)) >>> Z = X[:-1,:-1]**2 + Y[:-1,:-1] >>> plot(X, Y, Z) # doctest: +ELLIPSIS @@ -155,7 +157,7 @@ def plot(X, Y, Z, full=False, title=None, contours=None, interpolation=None, Y_min = Y[0,0] Y_max = Y[-1,-1] - fig = pylab.figure() + fig = matplotlib.pyplot.figure() if full: axes = fig.add_axes([0, 0, 1, 1]) else: @@ -252,7 +254,7 @@ def main(argv=None): p.add_option('--interpolation', dest='interpolation', default='nearest', help=('Interpolation scheme (for false color images) from %s ' '(%%default)') % ', '.join(interpolations)) - maps=[m for m in pylab.cm.datad if not m.endswith("_r")] + maps=[m for m in matplotlib.cm.datad if not m.endswith("_r")] maps.sort() p.add_option('-m', '--color-map', dest='cmap', default='jet', help='Select color map from %s (%%default)' % ', '.join(maps)) @@ -264,7 +266,7 @@ def main(argv=None): nx,ny = [int(x) for x in options.size.split(',')] try: - cmap = getattr(pylab.cm, options.cmap) + cmap = getattr(matplotlib.cm, options.cmap) except AttributeError: raise Exception('no color map named %s in %s' % (options.cmap, ', '.join(maps))) @@ -305,7 +307,7 @@ def main(argv=None): if options.output: fig.savefig(options.output) else: - pylab.show() + matplotlib.pyplot.show() if __name__ == '__main__': -- 2.26.2