How to remove values from HEATMAP
31 views (last 30 days)
Show older comments
I have data which have 4 column 14 rows, every value in table represent {NO, MIN, MAJ, MOD, COL} you can see the table in attach excel file. For developing heatmap I have converted these value into numbers such as (NO=0, MIN=0.25, MOD=0.50, MAJ=075, COL=1). I have developed the heat map the code is given below. I have 2 doubts regarding this
A) How to remove the values from the heatmap?
B) How to replace the numeric value with again {NO, MIN, MAJ, MOD, COL}. Ans also how to create a colorbar which shows color change with label {NO, MIN, MAJ, MOD, COL}.
Basically these are damage states means its increasing from NO to COL
%MATLAB CODE
s = [0, 1, 2, 3.6];
% file = "D:\OneDrive\WORK\VALID\value.xlsx";
file = "value.xlsx";
valueMap = containers.Map({'No', 'Min', 'Mod', 'Maj', 'Col'}, [0.00, 0.25, 0.5, 0.75, 1.00]);
tableData = readtable(file, 'ReadVariableNames', false);
numericData = zeros(size(tableData));
for i = 1:size(tableData, 1)
for j = 1:size(tableData, 2)
cellValue = char(tableData{i, j}); % Convert cell value to a string
if isKey(valueMap, cellValue)
numericData(i, j) = valueMap(cellValue);
else
numericData(i, j) = NaN;
end
end
end
disp(numericData);
vel = [4 4.25 4.5 4.75 5 5.25 5.5 5.75 6 6.25 6.5 6.75 7 7.25];
nColors = 256; % Number of colormap colors
customColormap = [linspace(1, 1, nColors)', linspace(0.8, 0, nColors)', linspace(0.8, 0, nColors)'];%RED
vel = fliplr(vel);
numericData = flipud(numericData);
h = heatmap(s, vel, numericData,'FontName', 'Times New Roman','fontsize',12);
colormap(customColormap);
0 Comments
Accepted Answer
Voss
on 25 Sep 2023
Edited: Voss
on 25 Sep 2023
I don't know of a way to alter the labels on a heatmap. What I would do in this situation is build a custom heatmap-looking plot using a patch object and text objects. Something like this:
% file = "D:\OneDrive\WORK\VALID\value.xlsx";
file = "value.xlsx";
s = [0, 1, 2, 3.6];
vel = [4 4.25 4.5 4.75 5 5.25 5.5 5.75 6 6.25 6.5 6.75 7 7.25];
% NO=0, MIN=0.25, MOD=0.50, MAJ=0.75, COL=1
data_levels = [0, 0.25, 0.5, 0.75, 1];
data_labels = {'NO','MIN','MOD','MAJ','COL'};
tableData = readtable(file, 'ReadVariableNames', false);
[~,idx] = ismember(upper(table2array(tableData)),data_labels);
numericData = data_levels(idx);
% nColors = 256; % Number of colormap colors
nColors = numel(data_levels); % Number of colormap colors
customColormap = [ones(nColors,1), repmat(linspace(0.8, 0, nColors),2,1).'];%RED
edges = [-Inf (data_levels(1:end-1)+data_levels(2:end))/2 Inf];
color_idx = discretize(numericData,edges);
nan_idx = isnan(numericData);
if any(nan_idx,'all')
nColors = nColors+1;
customColormap = [0 0 0; customColormap];
color_idx(nan_idx) = 0;
color_idx = color_idx+1;
cb_labels = [{'NaN'} data_labels];
else
cb_labels = data_labels;
end
labels = cb_labels(color_idx);
x = 0:4;
y = [vel(1)+0.5*(vel(1)-vel(2)), ...
(vel(1:end-1)+vel(2:end))/2, ...
vel(end)+0.5*(vel(end)-vel(end-1))];
xd = repelem(x((1:end-1)+[0;1;1;0;0]),1,numel(y)-1);
yd = repmat(y((1:end-1)+[0;0;1;1;0]),1,numel(x)-1);
figure();
patch('XData',xd,'YData',yd,'FaceColor','flat', ...
'FaceVertexCData',customColormap(color_idx,:))
[xt,yt] = meshgrid(x(1:end-1)+0.5,vel);
text(xt(:),yt(:),labels(:), ...
'FontWeight','bold','HorizontalAlignment','center')
xlim(x([1 end]))
ylim(y([1 end]))
xticks(x(1:end-1)+0.5)
xticklabels(s)
yticks(vel)
set(gca(),'FontWeight','bold')
colormap(customColormap);
cb = colorbar();
cb.YTick = linspace(1/(2*nColors),1-1/(2*nColors),nColors);
cb.YTickLabel = cb_labels;
2 Comments
Voss
on 28 Sep 2023
You're welcome!
Please post your solution. I'm curious to see alternative methods for modifying heatmaps.
More Answers (0)
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!