使用三种颜色,值范围为 0-100,颜色栏默认分别在 1/3 和 2/3 处分割颜色:
我想手动选择此间隔。例如。分别为 1/4 和 1/2。
MWE:
clc; close all; clear all
data = (50+50)*rand(10,3)
% green if acc(:,1)<Crit1
% red if acc(:,1)>Crit2
% yellow if acc(:,1)>Crit1 && acc(:,1)<Crit2
Crit1 = 25;
Crit2 = 50;
imagesc(data)
mycolormap = [0 1 0; 1 1 0; 1 0 0];
colormap(mycolormap)
colorbar
我之前曾发布过这个问题(https://math.stackexchange.com/posts/3472347/),可能是在错误的论坛上,但没有任何回复。
最佳答案
默认情况下,imagesc
会缩放数据并统一选择之间的阈值。要改变这一点,您有多种选择:
定义具有重复颜色的颜色图,以产生您想要的对应关系。要获得 1/4 和 1/2,您需要
mycolormap = [0 1 0; 1 1 0; 1 0 0; 1 0 0];
根据值之间的间隔,这会在颜色栏中给出不等长度:
手动应用阈值,然后更改颜色条标签:
data = 100*rand(10,3); thresholds = [0 1/4 1/2 1]; % thresholds for normalized data mycolormap = [0 1 0; 1 1 0; 1 0 0]; % colormap with numel(threholds)-1 rows m = min(data(:)); % min of data data_normalized = data-min(data(:)); r = max(data_normalized(:)); % range of data data_normalized = data_normalized./r; t = sum(bsxfun(@ge, data_normalized, reshape(thresholds, 1, 1, [])), 3); imagesc(t) colormap(mycolormap) h = colorbar; set(h, 'Ticks', 1:numel(thresholds)) set(h, 'Ticklabels', m+r*thresholds)
这会在颜色条中提供相等的长度,因此指示的值不会形成统一的比例。阈值相对于数据范围。
与上面相同,但具有绝对阈值:
data = 100*rand(10,3); thresholds = [0 25 50 100]; % absolute thresholds for data mycolormap = [0 1 0; 1 1 0; 1 0 0]; % colormap with numel(threholds)-1 rows t = sum(bsxfun(@ge, data, reshape(thresholds, 1, 1, [])), 3); imagesc(t) colormap(mycolormap) h = colorbar; set(h, 'Ticks', 1+linspace(0, 1, numel(thresholds))*(numel(thresholds)-2)) set(h, 'Ticklabels', thresholds)
关于matlab - 手动更改 Matlab 颜色条的颜色间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59373699/