手动更改 Matlab colorbars 的颜色间隔

Change Matlab colorbars' color interval manually

使用三种颜色,值范围为 0-100,Colorbar 默认将颜色拆分为 1/3 和 2/3:

我想 select 这个间隔手动。例如。在 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. 定义一个具有重复颜色的颜色图,以产生您想要的对应关系。要获得 1/4 和 1/2,您需要

    mycolormap = [0 1 0; 1 1 0; 1 0 0; 1 0 0];
    

    根据值之间的分隔,这会在颜色条中给出 不等长

  1. 手动应用阈值,然后更改颜色条标签:

    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)
    

    这会在颜色条中提供 等长 ,因此指示的值不会形成统一的比例。阈值 相对于 数据范围。

  1. 与上面相同,但具有 绝对 阈值:

    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)