#!/usr/bin/env python # # This is a demonstration of how to create image plots within python. # # from pylab import * figure(1,figsize=(15,10)) # set figure number (1) and size (inches: (X, Y)) # You can, of course, set any number of figures within matplotlib, and refer back to them, change them # whenever you want. . . also nice. #======================================================= # Plot 1: A fake image showing the imshow function, # the colour bar, the image cuts, etc. #======================================================= def func3(x,y): return (1- x/2 + x**5 + y**3)*exp(-x**2-y**2) # if you make these smaller, you increase the resolution dx, dy = 0.05, 0.05 x = arange(-3.0, 3.0, dx) y = arange(-3.0, 3.0, dy) X,Y = meshgrid(x, y) Z = func3(X, Y) #=============================================================== # Image 1: shows the basics of imshow, the image scaling (cuts) # the colour maps, and the interpolations. #=============================================================== subplot(131) Smin = Z.mean() - 2.0*Z.std() # vmin/vmax are the image cuts. If left out of imshow, the maximum range will be assumed. Smax = Z.mean() + 3.0*Z.std() imshow(Z, vmin=Smin, vmax=Smax, cmap=cm.jet, interpolation='bilinear',origin='upper') # cm.jet is the colour map (cm.hot, cm.cool, cm.summer, etc.) # origin='lower' puts image right-side-up. #imshow(Z, cmap=cm.jet, interpolation='nearest') # here, I have put these in so that you can try the different #imshow(Z, cmap=cm.jet, interpolation='bicubic') # 'nearest' is best for science data as it involves no smoothing. # Acceptable interpolations are: None, 'nearest', 'bilinear', # 'bicubic', 'spline16', 'spline36', 'hanning', 'hamming', 'hermite', # 'kaiser', 'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell', # 'sinc', and 'lanczos' title('An image using python and pylab') #=============================================================== # Image 2: the same image as above, but with contours. #=============================================================== subplot(132) imshow(Z, vmin=Smin, vmax=Smax, cmap=cm.jet, interpolation='nearest', origin='lower') contour_levels = arange(Smin, Smax, 0.2) # define some contour levels from Smin to Smax in steps of 0.2 contour(Z, contour_levels,cmap=cm.get_cmap('jet', len(contour_levels)-1)) # the contour levels can be any colour map you want (gray, or as here jet). title2 = 'The same image with contours and a different interpolation' title(title2, fontsize=9) x1, x2 = xlim() # here, I set the variables x1,x2 and y1,y2 to the plot limits. y1, y2 = ylim() text(x2+x2/50., 0, 'The image is also upside-down!', color='red', size=10, rotation='vertical') #========================================================================================= # Image 3: the same image as above, but with contours, another overplot, and a colour bar #========================================================================================= subplot(133) imshow(Z, vmin=Smin, vmax=Smax, cmap=cm.jet, interpolation='nearest', origin='lower') colorbar() # you can also add a colour bar to show the image intensity scale. contour_levels = arange(Smin, Smax, 0.2) # define some contour levels from Smin to Smax in steps of 0.2 contour(Z, contour_levels,cmap=cm.get_cmap('jet', len(contour_levels)-1)) # the contour levels can be any colour map you want (gray, or as here jet). # if I put the colorbar() here, then it would show the levels of the contours! x = arange(0,120,1) # here x = 0 to 120 in steps of 1. y = 50. + 50.*exp(-0.02*x) * cos(0.5*x) plot(x,y,'w', linewidth=2) title3 = 'The same image with an overplot and a colour bar' title(title3, fontsize=9) xlim(x1,x2) ylim(y1,y2) show()