representing discrete values by colors

Friends,
I have a simple array like this data=[0,0,1,1,3,1,0,1,1,3]. I want to make a plot (using pcolor/matshow) by giving distinct color to each integer. Something like green for zero, red for 1 and blue for 3. Your suggestions / any pseudo code would be of great help.

Thanks,
Bala

With ipython -pylab:

data=[0,0,1,1,3,1,0,1,1,3]
z = np.array(data, dtype=int)
z.shape = 2,5
cmap = mpl.colors.ListedColormap(['g', 'r', 'k', 'b'])
matshow(z, cmap=cmap)

If the argument to matshow or pcolor (or anything doing color mapping) is an ndarray of integers, then those integers are used directly to index into the lookup table of the colormap. ListedColormap provides a simple way of generating a colormap with a sequence of discrete colors.

Eric

ยทยทยท

On 06/14/2011 12:36 AM, Bala subramanian wrote:

Friends,
I have a simple array like this data=[0,0,1,1,3,1,0,1,1,3]. I want to
make a plot (using pcolor/matshow) by giving distinct color to each
integer. Something like green for zero, red for 1 and blue for 3. Your
suggestions / any pseudo code would be of great help.

Thanks,
Bala