1

I have a 2d numpy array obtained by reading from an image. The unique values of the array are 0, 1, and 2. I want to plot the image showing unique colors red, green, and blue for the values 0,1, and 2 respectively.

plt.imshow(data, cmap=colors.ListedColormap(['red'])

How would you do it?

Borys
  • 1,323
  • 6
  • 16
  • 40

1 Answers1

5
from matplotlib.colors import from_levels_and_colors
cmap, norm = from_levels_and_colors([0,1,2,3],['red','green','blue'])
plt.imshow(data, cmap=cmap, norm=norm)
Kirubaharan J
  • 2,255
  • 16
  • 23
  • 2
    It needs n+1 values for n colors, for eg 0 to 1 will be assigned red and 2 to 3 will be assigned blue,they are bounding intervals – Kirubaharan J Dec 02 '14 at 13:07