matlab - 手动更改 Matlab 颜色条的颜色间隔

标签 matlab colorbar

使用三种颜色,值范围为 0-100,颜色栏默认分别在 1/3 和 2/3 处分割颜色:

enter image description here

我想手动选择此间隔。例如。分别为 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];
    

    根据值之间的间隔,这会在颜色栏中给出不等长度:

enter image description here

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

    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)
    

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

  • enter image description here

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

    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/

    相关文章:

    python - 使用 matplotlib 3.3+ 更改颜色条限制以更改比例

    python - 使 plt.colorbar 扩展到 vmin/vmax 前后的步骤

    python - 自定义多色水平条形图 matplotlib

    unix 命令行中类似 Matlab 的命令历史检索

    matlab - 典型相关分析

    matlab - 等效于 Lua 解释器的 Matlab "whos"命令?

    r - 调整 R 中图像图的色标

    matlab - 插值结果如何大于或小于原始信号的最小值/最大值?

    matlab - 如何在 MATLAB 中重命名循环中的变量?

    python - ImageGrid 中每一行的颜色条