I have a 2D array that I’m plotting with imshow and I would like to have costums colors depending on the value of each pixel of my array. I’ll explain it with an example.
from pylab import *
from numpy import *
img = ones((5,5))
img[1][1] = 2
imshow(img,interpolation='nearest');colorbar()
If you ran this code you would see a red square in a blue background. The red square corresponds to the pixel [1][1] in img, while the other pixel are colored blue because they have a value of 1. What if I want the red square to be colored with a custom color?
Or more generally, if I have a 2D array like img in the example, how can I color pixel with the same value with a color I can choose.
I have found this page that explains how to generate a custom colorbar but that’s not useful: http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps
That link you sent has the following:
Isn’t this exactly what you want?
Here’s an example of how to do it with the image you provided:
Also, if you really want to map a number to a colour you can use discrete_cmap as specified in that example you linked to, here’s the example method the scipy documentation provides: