heatmap NaN Color replacement BUG

10 views (last 30 days)
Hi Community!
I am trying to grey out diagonals (for ease of viewing-0 being white was not sufficient for my collaborators) without modifying my color map to a custom color map, but am running across a curious issue; here is my code:
heatmap(a_y(1:2,2:3), MWx, MWy, ...
'%0.2f', 'TickAngle', 45, 'Colormap', redblue(100), 'FontSize', 25, 'NaNColor', [0.5 0.5 0.5])
title('Log 10 Relationships of Median Y_{1/2} MDA231 Note: (x/y)')
ax = gca;
ax.FontSize = 16;
ax.FontName = 'Arial';
clim([-0.10 0.10])
colorbar
This produces a heat map that looks incorrect, based on the color bar minimum and maximum value. In fact it treats clim as if it doesn't actually exist for the coloring of the heatmap, but does exist for the colorbar.
alternatively, if I use the code and remove the 'NanColor':
a_y = log10(PC3_y./trans_PC3_y);
a_y(sub2ind(size(a_y), 1:length(a_y), 1:length(a_y))) = NaN; %NaN on diagonals
figure
heatmap(a_y(1:2,2:3), MWx, MWy, ...
'%0.2f', 'TickAngle', 45, 'Colormap', redblue(100), 'FontSize', 25)
title('Log 10 Relationships of Median Y_{1/2} PC3 Note: (x/y)')
ax = gca;
ax.FontSize = 16;
ax.FontName = 'Arial';
clim([-0.10 0.10])
colorbar
I get a correct image, however NaN is considered the minimum value, which just makes a snap interpretation of this more difficult:
I even attempted to adjust the code of heatmap.m, where the code states:
p.addParamValue('NaNColor', [NaN NaN NaN], @(x)isnumeric(x) && length(x)==3 && all(x>=0) && all(x<=1)); %I adjusted [NaN NaN NaN] to 0.5 for all to make it grey
While it was successful in greying out the 1 diagonal in this abreviated heatmap, it messed up the heatmap's coloring just like the first image did. Does anyone know how to grey out without running into that problem?
Thanks,
Nick

Accepted Answer

Austin M. Weber
Austin M. Weber on 18 Jan 2024
Edited: Austin M. Weber on 18 Jan 2024
Instead of using the NanColor name-value pair (which I don't even see in the documentation?), you can try saving the plot to an axis handle and use dot indexing to set the MissingDataColor property to the NaN color that you want:
a_y = [-0.03 -0.08; NaN -0.05];
figure
h = heatmap(a_y);
% Quick redblue color map
redblue = flip([1 0 0; 1 0.25 0.25; 1 0.5 0.5; 1 0.75 0.75; 1 1 1; 0.75 0.75 1; 0.5 0.5 1; 0.25 0.25 1; 0 0 1]);
colormap(redblue)
h.MissingDataColor = [0.5 0.5 0.5];
clim([-0.10 0.10])

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!