Borys
Borys

Reputation: 1423

Show unique color for unique value

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?

Upvotes: 2

Views: 2197

Answers (1)

Kirubaharan J
Kirubaharan J

Reputation: 2365

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)

Upvotes: 6

Related Questions