Do not show null values ​​on a 2D heat map

I want to build a two-dimensional map of a syllicon plate. Therefore, only the central part has a value, and the corners have a value of 0. I use matplotlib plt.imshow to get a simple map as follows:

data = np.array([[ 0. ,  0. ,  1. ,  1. ,  0. ,  0. ],
       [ 0. ,  1. ,  1. ,  1. ,  1. ,  0. ],
       [ 1. ,  2. ,  0.1,  2. ,  2. ,  1. ],
       [ 1. ,  2. ,  2. ,  0.1,  2. ,  1. ],
       [ 0. ,  1. ,  1. ,  1. ,  1. ,  0. ],
       [ 0. ,  0. ,  1. ,  1. ,  0. ,  0. ]])

plt.figure(1)
plt.imshow(data ,interpolation='none')
plt.colorbar()

And I get the following card: enter image description here

Is there a way to remove the dark blue areas where the values ​​are zeros while maintaining the shape of the β€œplate” (green, red, and light blue areas)? The value of the corners would be white, while the rest would preserve the color configuration.

Or is there a better function I could use to get this?

+4
source share
1 answer

There are two ways to get rid of the blue corners:

:

data[data == 0] = np.nan
plt.imshow(data, interpolation = 'none', vmin = 0)

imshow:

data_masked = np.ma.masked_where(data == 0, data)
plt.imshow(data_masked, interpolation = 'none', vmin = 0)

, , .

, vmin/vmax . vmin = 0 plt.imshow , . masked-imshow

+3

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


All Articles