I've an array that includes decent observations, irrelevant observations (that I would like to mask out), and areas where there are no observations (that i would also like to mask out). I want to display this array as an image (using pylab.imshow) with two separate masks, where each mask is shown in a different colour.
I've found code for a single mask (here) in a certain colour, but nothing for two different masks:
masked_array = np.ma.array (a, mask=np.isnan(a))
cmap = matplotlib.cm.jet
cmap.set_bad('w',1.)
ax.imshow(masked_array, interpolation='nearest', cmap=cmap)
If possible, I'd like to avoid having to use a heavily distorted colour map but accept that that is an option.
You might simply replace values in you array with some fixed value depending on some conditions. For example, if you want to mask elements larger than 1 and smaller than -1:
val1, val2 = 0.5, 1
a[a<-1]= val1
a[a>1] = val2
ax.imshow(a, interpolation='nearest')
val1
and val2
can be modified to obtain colors you wish.
You can also set the colors explicitly, but it requires more work:
import matplotlib.pyplot as plt
from matplotlib import colors, cm
a = np.random.randn(10,10)
norm = colors.normalize()
cmap = cm.hsv
a_colors = cmap(norm(a))
col1 = colors.colorConverter.to_rgba('w')
col2 = colors.colorConverter.to_rgba('k')
a_colors[a<-0.1,:] = col1
a_colors[a>0.1,:] = col2
plt.imshow(a_colors, interpolation='nearest')
plt.show()