Display multiple masks in different colors in pylab

I have an array that contains decent observations, irrelevant observations (which I would like to mask), and areas where there are no observations (which I would also like to mask). I want to display this array as an image (using pylab.imshow) with two separate masks, where each mask is displayed in a different color.

I found the code for one mask ( here ) in a specific color, 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 would like to avoid using a very distorted color map, but I admit that this is an option.

+4
source share
3 answers

You can simply replace the values ​​in your array with some fixed value depending on some conditions. For example, if you want to hide elements larger than 1 and less 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 changed to get the desired colors.

You can also set colors explicitly, but this 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() 
+3
source

For me, the easiest way is to directly print masks with imshow, transfer different color palettes. The maximum and minimum number of colors are used for True and False values:

 mask1=np.isnan(a) mask2=np.logical_not(mask1) plt.imshow(mask1,cmap='gray') plt.imshow(mask2,cmap='rainbow') 

enter image description here

However, this (and other proposed approaches) also lays down False values ​​that override previous graphs. If you want to avoid building False values, you can do this by replacing them with np.nan after converting the array to float (np.nan is of type float and cannot be contained in a boolean mask). nan values ​​are not displayed:

 mmm=mask.astype(np.float) mmm[np.where(mmm==0)]=np.nan #the substitution can be done also in one line with: #mmm=np.where(mask,mask.astype(np.float),np.nan) plt.imshow(mmm,cmap='rainbow',vmin=0,vmax=1)) #will use only the top color: red. vmin and vmax are needed if there are only one value (1.0=True) in the array. plt.colorbar() #repeat for other masks... 

enter image description here And I hope that I will not move on to the topic, but the same method can be used to build a different part of the data with different color maps, replacing the graph plotting command:

 plt.imshow(mmm*data,cmap='rainbow') 

enter image description here

+1
source

I don’t know what values ​​are in your array, but you can convert masked areas so that X values ​​become RGB (A) values ​​(tuples (R,G,B,A) ), in which case cmap is ignored, according to the documentation , at least.

• cmap: [No | Colormap]

Example matplotlib.colors.Colormap, for example. cm.jet. If None, the default value is rc image.cmap. cmap is ignored when X has RGB (A) information

0
source

Source: https://habr.com/ru/post/1446055/


All Articles