Changing the colours in heatmap for specific ranges in values

조회 수: 12 (최근 30일)
Ryan Verburg
Ryan Verburg 2024년 10월 17일
댓글: Voss 2024년 10월 18일
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.

채택된 답변

Voss
Voss 2024년 10월 17일
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;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by