import optparse
import sys
+import matplotlib
+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.
-#import matplotlib
#matplotlib.use('Agg') # select backend that doesn't require X Windows
#matplotlib.use('GTKAgg') # select backend that supports pylab.show()
X,Y = pylab.meshgrid(Xs, Ys)
return (X,Y,Z)
-def plot(X, Y, Z, full=False, title=None, contours=None, cmap=None):
+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))
fig = pylab.figure()
if full:
- axes = pylab.axes([0, 0, 1, 1])
+ axes = fig.add_axes([0, 0, 1, 1])
else:
- axes = pylab.axes()
+ axes = fig.add_subplot(1, 1, 1)
if title:
axes.set_title(title)
axes.set_axis_off()
if contours:
cset = axes.contour(X[:-1,:-1], Y[:-1,:-1], Z, contours, cmap=cmap)
# [:-1,:-1] to strip dummy last row & column from X&Y.
- pylab.clabel(cset, inline=1, fmt='%1.1f', fontsize=10)
+ axes.clabel(cset, inline=1, fmt='%1.1f', fontsize=10)
else:
# pcolor() is much slower than imshow.
#plot = axes.pcolor(X, Y, Z, cmap=cmap, edgecolors='none')
#axes.autoscale_view(tight=True)
- plot = axes.imshow(Z, aspect='auto', interpolation='bilinear',
+ plot = axes.imshow(Z, aspect='auto', interpolation=interpolation,
origin='lower', cmap=cmap,
extent=(X_min, X_max, Y_min, Y_max))
if not full:
return fig
+def get_possible_interpolations():
+ try: # Matplotlib v1.0.1
+ return sorted(matplotlib.image.AxesImage._interpd.keys())
+ except AttributeError:
+ try: # Matplotlib v0.91.2
+ return sorted(matplotlib.image.AxesImage(None)._interpd.keys())
+ except AttributeError:
+ # give up ;)
+ pass
+ return ['nearest']
+
def test():
import doctest
results = doctest.testmod()
Title: Some like it hot
Image size: 5 2
False color
- X range: 0 4
- X range: 0 1
+ X range: 0 4 (6 steps)
+ Y range: 0 1 (3 steps)
Z range: 0.0 9.0
>>> img = o.read()
>>> img.startswith('\\x89PNG')
help='Title (%default)')
p.add_option('--test', dest='test', action='store_true',
help='Run internal tests and exit.')
+ interpolations = get_possible_interpolations()
+ 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.sort()
p.add_option('-m', '--color-map', dest='cmap', default='jet',
Z_min = numpy.min(Z.flat)
Z_max = numpy.max(Z.flat)
- print 'X range: ', X[0,0], X[0,-2]
- print 'X range: ', Y[0,0], Y[-2,0]
+ print 'X range: {} {} ({} steps)'.format(
+ X[0,0], X[0,-2], X.shape[1])
+ print 'Y range: {} {} ({} steps)'.format(
+ Y[0,0], Y[-2,0], Y.shape[0])
print 'Z range: ', Z_min, Z_max
fig = plot(X, Y, Z, full=options.full, title=options.title,
- contours=options.contours, cmap=cmap)
+ contours=options.contours, interpolation=options.interpolation,
+ cmap=cmap)
if options.output:
fig.savefig(options.output)
else:
- pylab.ion()
pylab.show()