Colormap- specific values for each heatmap value

62 visualizzazioni (ultimi 30 giorni)
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;

Risposta accettata

Voss
Voss il 31 Mag 2022
Modificato: Voss il 31 Mag 2022
Your idea of replicating the same color a certain number of times to build a colormap is good. However, the number of times each color is repeated/replicated should not be the number of data elements within the corresponding range for that color.
Instead, since the "thresholds" at which the colors change (i.e., 0.01, 0.06, 0.14) are fixed, and since a colormap in MATLAB must use equally-spaced values (i.e., each color in the colormap is applied to a data range of equal size), the number of times each color in your colormap must be repeated is based on the difference between the thresholds only, not the data that the colormap will be applied to.
Here's one way to do it (the code that constructs the colormap is in the function build_colormap, defined below, which is convenient since I'm going to call it a few times - you don't have to have it in a function - and I also put the plotting code into its own function, plot_heatmap):
max_val = 0.15;
% random data between 0 and max_val:
Data = max_val*rand(15,1);
% construct the colormap:
cmap = build_colormap(max_val);
%plots heatmap with cmap as colormap
plot_heatmap(Data,cmap,max_val);
Now the colormap (and colorbar) is the same no matter what the data is (for a given maximum data value):
max_val = 0.15;
% random data between 0 and max_val:
Data = max_val*rand(2,1);
% construct the colormap:
cmap = build_colormap(max_val);
%plots heatmap with cmap as colormap
plot_heatmap(Data,cmap,max_val);
max_val = 0.55; % higher max_val than before, but same thresholds (0.01, 0.06, 0.14)
% random data between 0 and max_val:
Data = max_val*rand(25,1);
% construct the colormap:
cmap = build_colormap(max_val);
%plots heatmap with cmap as colormap
plot_heatmap(Data,cmap,max_val);
function cmap = build_colormap(max_val)
% specify the colors used to be used in the colormap:
colors = [ ...
1 1 1; ...
0.6 0.6 0.6; ...
0.7 0.7 0.7; ...
0.6941 0.6353 0.7922; ...
];
% specify the levels at which the color changes:
c_levels = [0 0.01 0.06 0.14 max_val];
% count how many 0.01 increments fit between each pair of levels:
n_colors = round(diff(c_levels)/0.01);
% create the colormap:
cmap = [];
for ii = 1:numel(n_colors)
cmap = [cmap; repmat(colors(ii,:),n_colors(ii),1)];
end
end
function plot_heatmap(Data,cmap,max_val)
%plots heatmap with cmap as colormap
figure();
h=heatmap(Data);
h.Colormap = cmap;
% set the clim:
clim([0 max_val]);
end

Più risposte (1)

DGM
DGM il 31 Mag 2022
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)
CT = 256×3
0.2422 0.1504 0.6603 0.2422 0.1504 0.6603 0.2422 0.1504 0.6603 0.2422 0.1504 0.6603 0.2422 0.1504 0.6603 0.2422 0.1504 0.6603 0.2422 0.1504 0.6603 0.2422 0.1504 0.6603 0.2422 0.1504 0.6603 0.2422 0.1504 0.6603
% use it
heatmap(z);
colormap(CT)
caxis([min(cbreaks) max(cbreaks)])
You might also want to adjust the colorbar ticks as needed.

Categorie

Scopri di più su Colormaps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by