Changing the colours in heatmap for specific ranges in values

7 views (last 30 days)
I have a heatmap with data extending from about -120 to 1400. I am using colormap Turbo, which looks nice.
However, to make the graph more clear, I would like to have values below 0 and those above 1000 be a different colour, to make them stand out. For example, below 0 can be dark green and above 1000 can be purple. I have tried to look at other peoples code on different websites, but I cant seem to understand the answers.
I am already using NaN values to be grey if they are present, and since this wouldnt fix 2 thresholds for different colours, I cant use this method to colour these parts.
In any case, here is my data and plotting method.
xvalues = {'0 cm','1 cm','5 cm','10 cm','15 cm','20 cm','24 cm','25.3 cm'};
yvalues = {'30 mm','25 mm','20 mm','15 mm','10 mm','5 mm', '0 mm','-5 mm','-10 mm','-15 mm','-20 mm', '-25 mm','-30 mm','-35 mm','-40 mm','-45 mm','-50 mm','-55 mm','-60 mm', '-65 mm','-70 mm','-75 mm'};
h = heatmap(xvalues,yvalues,Data_avg,'CellLabelColor','none', 'Colormap', turbo,'MissingDataColor',[0.8 0.8 0.8]);
Thanks in advance for any advice.

Accepted Answer

Voss
Voss on 17 Oct 2024
Here's one way:
xvalues = {'0 cm','1 cm','5 cm','10 cm','15 cm','20 cm','24 cm','25.3 cm'};
yvalues = {'30 mm','25 mm','20 mm','15 mm','10 mm','5 mm', '0 mm','-5 mm','-10 mm','-15 mm','-20 mm', '-25 mm','-30 mm','-35 mm','-40 mm','-45 mm','-50 mm','-55 mm','-60 mm', '-65 mm','-70 mm','-75 mm'};
% some made-up data
Data_avg = linspace(0,1,numel(xvalues)).*linspace(0,1,numel(yvalues)).'*1520-120;
h = heatmap(xvalues,yvalues,Data_avg,'CellLabelColor','none','MissingDataColor',[0.8 0.8 0.8]);
% limits beyond which to apply limit_colors
limits = [0 1000];
% green and purple
limit_colors = [0 0.6 0; 0.6 0 0.6];
% adjust the colormap to include limit_colors beyond limits
cmap = turbo();
NC = size(cmap,1);
idx = (limits-h.ColorLimits(1))/diff(h.ColorLimits)*NC;
idx = [max(0,floor(idx(1))) min(NC+1,ceil(idx(2)))];
cmap(1:idx(1),:) = repmat(limit_colors(1,:),idx(1),1);
cmap(idx(2):end,:) = repmat(limit_colors(2,:),NC-idx(2)+1,1);
h.Colormap = cmap;

More Answers (0)

Categories

Find more on Colormaps 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!