Matlab imshow omit NaN

You can set the AlphaData of the image object to be equal to ~isnan(data) such that NaN’s will be shown as transparent values.

R = rand(10);
R(R < 0.25) = NaN;

him = imshow(R, 'InitialMagnification', 10000);
colormap parula
set(him, 'AlphaData', ~isnan(R))

enter image description here

If you want a specific color, you could turn on the axes and set the color of the axes to be whatever color you want the NaN values to be.

axis on;

% Make a red axis
set(gca, 'XColor', 'none', 'yColor', 'none', 'xtick', [], 'ytick', [], 'Color', 'r')

enter image description here

If you use pcolor, then NaN values are already treated as transparent.

Leave a Comment