Matplotlib imshow: how to apply a mask to a matrix

I am trying to analyze graphically 2d data. matplotlib.imshowvery useful in this, but I feel that I can use it even more if I could exclude some cells from my matrix, values ​​outside the range of interests. My problem is that these values ​​“smooth out” the color set in my range of interests. I could have a higher color resolution after excluding these values.

I know how to apply a mask to my matrix to exclude these values, but after applying the mask, it returns a 1d object:

mask = (myMatrix > lowerBound) & (myMatrix < upperBound)
myMatrix = myMatrix[mask] #returns a 1d array :(

Is there a way to pass the mask in imshowhow to restore a 2d array?

+4
source share
1

numpy.ma.mask_where , .

import numpy as np
import matplotlib.pyplot as plt

lowerBound = 0.25
upperBound = 0.75
myMatrix = np.random.rand(100,100)

myMatrix =np.ma.masked_where((lowerBound < myMatrix) & 
                             (myMatrix < upperBound), myMatrix)


fig,axs=plt.subplots(2,1)
#Plot without mask
axs[0].imshow(myMatrix.data)

#Default is to apply mask
axs[1].imshow(myMatrix)

plt.show()
+6

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


All Articles