Print the number of steps read in by plot_image.py (for sanity checks).
[parallel_computing.git] / src / plot_image / plot_image.py
index 9eaaa205d30cbcbdf75ef91b9e338d172aa99c7b..11a3a9787b4a8b0aadf2b6718570a28b951008ff 100755 (executable)
@@ -54,11 +54,12 @@ For other ideas, see the Matplotlib website [2]_.
 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()
 
@@ -134,7 +135,8 @@ def read_data_3d(stream):
     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))
@@ -149,9 +151,9 @@ def plot(X, Y, Z, full=False, title=None, contours=None, cmap=None):
 
     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()
@@ -159,12 +161,12 @@ def plot(X, Y, Z, full=False, title=None, contours=None, cmap=None):
     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:
@@ -172,6 +174,17 @@ def plot(X, Y, Z, full=False, title=None, contours=None, cmap=None):
     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()
@@ -191,8 +204,8 @@ def main(argv=None):
     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')
@@ -229,6 +242,10 @@ def main(argv=None):
                  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',
@@ -269,17 +286,19 @@ def main(argv=None):
 
     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()