0

suppose I have a 2D matrix of 1s and 0s and I plot it with imshow:

import matplotlib.pyplot as plt
plt.imshow([[1,0],[0,1]])

I would get the following output

enter image description here

Is there an easy way to map the colours to specific values? for example the 0 to grey and the 1 to blue? I presume something like

from matplotlib.colors import ListedColormap
cm = ListedColormap(['grey', 'blue'])
plt.imshow([[1,0],[0,1]], cmap=cm) 

However that misses the mapping with the values. Couldn't find anything straightforward on SO.

lorenzori
  • 737
  • 7
  • 23
  • 1
    I run your code and get [this image](https://i.stack.imgur.com/MCVnA.png) and it seems fine. What do you mean by _However that misses the mapping with the values_? – Zephyr Sep 02 '21 at 19:55
  • `NameError: name 'ListedColormap' is not defined` always include all relevant imports – Trenton McKinney Sep 02 '21 at 20:12
  • @Zephyr it means that 0 won't be mapped to grey and 1 to blue. For example if I invert the colormap to `'blue', 'grey'` the colours will be inverted, independent of the values. – lorenzori Sep 03 '21 at 07:28
  • added the `ListedColormap` import – lorenzori Sep 03 '21 at 07:29

0 Answers0