Heatmap - multiple values in same cell

15 views (last 30 days)
Tobias Wendlinger
Tobias Wendlinger on 8 Mar 2022
Answered: Simon Chan on 8 Mar 2022
Hello,
is it possible to display multiple values in the same heatmap "cell"?
For example I have 3 different entries for a y-value of -51 and a x-value of 1. Can this be plotted similarly to this?
I tried doing it with the heatmap() function, but matlab only displayed one of the three entries for y = -51 & x = 1.
Thanks in advance for your help

Answers (2)

Bjorn Gustavsson
Bjorn Gustavsson on 8 Mar 2022
You can convert three-valued data into an RGB-image. The simplest way to go about this is something along these lines:
dataRGB = zeros(size(HMdata,1)/3,size(HMdata,2),3); % Here I assume that each cell in y corresponds to 3 values
dataRGB(:,:,1) = HMdata(1:3:end,:);
dataRGB(:,:,2) = HMdata(2:3:end,:);
dataRGB(:,:,3) = HMdata(3:3:end,:);
To easily display this type of plot as an image we will have to normalize the values to the 0-1 range. You can do that for all three components together:
imRGB1 = normalize(dataRGB,'range',[0 1]);
or for each RGB-layer separately:
imRGB3(:,:,3) = normalize(dataRGB(:,:,3),'range',[0 1]);
imRGB3(:,:,2) = normalize(dataRGB(:,:,2),'range',[0 1]);
imRGB3(:,:,1) = normalize(dataRGB(:,:,1),'range',[0 1]);
Then you can display it using for example:
imagesc(imRGB1)
HTH

Simon Chan
Simon Chan on 8 Mar 2022
Not writing data to the same cell, but writing to separate cells as a workaround:
Manually change the YDisplayLabel to let the heatmap similar to what you want.
data = rand(18,3);
h = heatmap(data);
yvalue = [-20,-39,-43,-47,-51,-55];
emptystr = repelem({''},1,length(yvalue));
numstr = arrayfun(@(x) num2str(x),yvalue,'uni',0);
h.YDisplayLabels = reshape(vertcat(emptystr,numstr,emptystr),[],1);

Community Treasure Hunt

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

Start Hunting!