By the way, I had some success with this, but my approach is really inefficient. Perhaps someone could suggest how I can improve the following:
import matplotlib.numerix as nx from numarray import ma def enumimage(A,colors): """ Create an image from a matrix of enumerated values A is a matrix colors is a dictionary mapping enumerated values to (r,g,b,a) tuples If a value in A is masked, the resulting image will be transparent If a value in A is not present in the color map, it will plot black. """ B = nx.zeros((nx.size(A,0),nx.size(A,1),4)) mask = A.mask() for r in range(nx.size(A,0)): for c in range(nx.size(A,1)): v = A[r,c] if mask[r,c]: B[r,c,:]=[1,1,1,0] else: try: B[r,c,:]=colors[v] except KeyError,e: B[r,c,:]=[0,0,0,1] return B if __name__=='__main__': import pylab A = nx.array([[1,2,7],[1,2,2],[1,1,9]]) print "A =",A A1 = ma.array(A,mask=ma.where(A==1,1,0)) print "A1 =",A1 colors={ 2:(1,0,0,1) ,7:(0,1,0,1) } B = enumimage(A1,colors) print "B =",B pylab.figure() pylab.hold() pylab.imshow(B,interpolation='nearest',extent=0.5+nx.array([0,nx.size(A,0),nx.size(A,1),0])) pylab.axes pylab.show() John Pye wrote: > Hi all > > I have some data with enumerated values in an array. Values are like > 1,2,7,9 spread around in the array. I want to plot those values so that > I can see the 'regions' in my data, then I want to overlay this with > some contour lines drawn from other data. > > I want to be able to specify the colors for each of the enumerated > values, as they need to be consistent throughout my work. My array of > enumerated values is *masked* so that there are areas where I don't want > to show anything (this would plot as transparent pixels). > > Has anyone got suggestions on the best way of doing this? It seems that > the technique in > http://www.scipy.org/Cookbook/Matplotlib/Plotting_Images_with_Special_Values > might be overkill, right? It also seemed that it had some problems with > masked arrays. > > Cheers > > JP > > -- John Pye School of Mechanical and Manufacturing Engineering The University of New South Wales Sydney NSW 2052 Australia t +61 2 9385 5127 f +61 2 9663 1222 mailto:john.pye_AT_student_DOT_unsw.edu.au http://pye.dyndns.org/ _______________________________________________ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users