Colormap- specific values for each heatmap value
Show older comments
Hey, I am currently struggeling with plotting different colors for different value ranges in a heatmap.
So I have a specific number of values in Data and want to plot all values below 0.01 in one color, values between 0.01 and 0.06 in a different color and so on. clim does not work, since it interpolates between the upper and lower value and i do not have only 2 limits.
I thought maybe, if I only give n-many colorcodes in the colormap for n-numbers in data it might work, but it does not.
Maybe you have a suggestion.
Thanks a lot !
%finds the amount of values below 0.01, between 0.01 and 0.06, between 0.06
%and 0.14 and larger than 0.14 in Data
noEffect = numel(find(Data < 0.01));
smallEffect = numel(find(Data < 0.06)) - numel(find(Data <= 0.01));
mediumEffect = numel(find(Data < 0.14)) - numel(find(Data <= 0.06));
largeEffect = numel(find(Data >= 0.14));
% creates a colormap depending on the number of
% no/small/medium/large effects
cmap = [];
cmap(1:noEffect,1:3) = 1;
cmap(end+1:end + smallEffect,1:2) = 0.6;
cmap(end+1 - smallEffect:end ,3) = 0.6;
cmap(end+1:end + mediumEffect,1) = 0.7;
cmap(end+1 - mediumEffect : end ,2) = 0.7;
cmap(end+1 - mediumEffect : end ,3) = 0.7;
cmap(end+1:end + largeEffect,1) = 0.6941;
cmap(end+1 - largeEffect : end ,2) = 0.6353;
cmap(end+1 - largeEffect : end,3) = 0.7922;
%plots heatmap with cmap as colormap
h=heatmap(Data);
h.Colormap = cmap;
Accepted Answer
More Answers (1)
You might try something like this:
% some fake data
z = 0.2*rand(10);
% define parameters of new CT
ctlength = 256;
cbreaks = [0 0.01 0.06 0.15 max(z(:))];
basecolors = parula(4); % or any Mx3 CT that matches length of cbreaks
% construct a discrete CT
idxbreaks = round(1 + (ctlength-1)*cbreaks/cbreaks(end));
blocklen = diff(idxbreaks);
blocklen(end) = blocklen(end)+1;
CT = repelem(basecolors,blocklen,1)
% use it
heatmap(z);
colormap(CT)
caxis([min(cbreaks) max(cbreaks)])
You might also want to adjust the colorbar ticks as needed.
1 Comment
Heinz Leon
on 1 Jun 2022
Categories
Find more on Color and Styling 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!


