I'm trying to use the matplotlib.imshow to plot a boolean 2d array and I'd like to be able to pick the two colours (one for true and one for false). It seems that this argument should be passed as a colormap to imshow but in my case it seems a bit of an overkill as I don't need continuous and, more important, I don't know how to define a custom colormap (the matplotlib doc hasn't helped me with that).
Asked
Active
Viewed 8,150 times
10
Learning is a mess
- 5,905
- 5
- 30
- 64
-
can you link to the doc that you were reading and explain what wasn't clear? – Paul H Oct 28 '14 at 20:41
-
1You can make color maps with a discrete number of steps, just use 2 for binary data – tacaswell Oct 28 '14 at 20:49
-
http://stackoverflow.com/questions/14777066/matplotlib-discrete-colorbar – tacaswell Oct 28 '14 at 20:50
1 Answers
18
import matplotlib.pyplot as plt
import matplotlib.colors
# Color for False and True
cmap = matplotlib.colors.ListedColormap(['red', 'green'])
plt.imshow([True, False], [False, True]], cmap=cmap)
DieterDP
- 3,531
- 2
- 27
- 36
-
2
-
9Indeed, 6years since I asked that. Finally I can close this and move on with my life =) – Learning is a mess Dec 15 '20 at 14:46