Q:

Map discrete value to color

I am trying to create pcolor based on 4 discrete value 1, 2, 3, 4. I want to define 1 as black, 2 as red, 3 as yellow and 4 as green. does anyone know how to do it?
test = ([1,2,2,1,3],[1,1,1,1,4],[2,1,1,2,1])
import numpy as np

dataset = np.array(test)
plt.pcolor(dataset)

Thanks,

A:

I don't think you want to use pcolor. From the Matlab docs (presumably the matplotlib pcolor does the same thing):

The minimum and maximum elements of C are assigned the first and last colors in the colormap. Colors for the remaining elements in C are determined by a linear mapping from value to colormap element.

You could try imshow instead, and use a dict to map the colors you want:
colordict = {1:(0,0,0),2:(1,0,0),3:(1,1,0),4:(0,1,0)}
test = ([1,2,2,1,3],[1,1,1,1,4],[2,1,1,2,1])
test_rgb = [[colordict[i] for i in row] for row in test]
plt.imshow(test_rgb, interpolation = 'none')

