在 MATLAB 中控制颜色条比例 [英] Control colorbar scale in MATLAB

查看:49
本文介绍了在 MATLAB 中控制颜色条比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:如何在自定义 MATLAB 颜色栏中指定颜色过渡?
具体来说,我想让黄色(见下文)覆盖颜色条的更多区域(也许 [19.5–21.5] 或接近那个区域).

使用

完整的代表性示例如下

% MATLAB 2017a% 数据X = [22.6 22.8 22.6 20.45 22.3 18.15 19.95 20.8].';Y = [84 89 63 81 68 83 77 52].';Z = [23.0 22.695 21.1450 21.5 22.09 20.5 22.075 20.915].';% 创建自定义颜色图% 参考:https://stackoverflow.com/questions/24488378/how-to-map-a-specific-value-into-rgb-color-code-in-matlab/24488819#24488819col3 = [0 1 0];%Gcol2 = [1 1 0];%Ycol1 = [1 0 0];%Rn1 = 20;n2 = 20;cmap=[linspace(col1(1),col2(1),n1);linspace(col1(2),col2(2),n1);linspace(col1(3),col2(3),n1)];cmap(:,end+1:end+n2)=[linspace(col2(1),col3(1),n2);linspace(col2(2),col3(2),n2);linspace(col2(3),col3(3),n2)];cmap = cmap.';% 阴谋颜色图(cmap),坚持,框p = scatter(X,Y,[],Z,'filled','DisplayName','Data3');cb = 颜色条;cb.Limits = [18 23];cb.Ticks = [18:1:23];% 化妆品p.MarkerEdgeColor = 'k';xlabel('X')ylabel('Y')cb.Label.String = 'Z';

解决方案

我认为您缺少的只是调用

注意下面这行...

cb.Limits = [18 23];

... 仅更改 刻度限制 显示在颜色栏上,但不会改变关于数据如何映射到颜色范围的任何内容.caxis 函数是你控制它的方式(在上面的例子中,将 18 的值映射到一端,将 23 的值映射到另一端).默认情况下,您的代码将 Z 中的最小值和最大值映射到颜色范围(分别为 20.5 和 23).当您将颜色条上的刻度限制设置为更大的范围时,它只是用颜色图中的最后一种颜色填充它,在本例中为红色.这就是为什么你看到这么多.

奖金

仅仅因为您可能感兴趣,您也可以通过interp1 函数可以轻松生成您的颜色图,如下所示:

cmap = interp1([1 0 0; 1 1 0; 0 1 0], linspace(1, 3, 41));

Question: How do I specify color transitions in a custom MATLAB colorbar?
Specifically, I'd like to make the yellow (see below) cover more area of the colorbar (perhaps [19.5–21.5] or something close to that).

Using this answer, I was able to create a custom colorbar in MATLAB. I'm trying to understand this answer as it might be relevant.

I have attempted approaches from this answer and reviewed this answer & this one and was unable to accomplish my goal.

It is clear I am missing something.

Full representative example below

% MATLAB 2017a
% Data
X = [22.6 22.8 22.6 20.45 22.3 18.15 19.95 20.8].';
Y = [84 89 63 81 68 83 77 52].';
Z = [23.0 22.695 21.1450 21.5 22.09 20.5 22.075 20.915].';

% Create custom colormap  
% Reference: https://stackoverflow.com/questions/24488378/how-to-map-a-specific-value-into-rgb-color-code-in-matlab/24488819#24488819
col3 = [0 1 0]; %G
col2 = [1 1 0]; %Y
col1 = [1 0 0]; %R
n1 = 20; n2 = 20;
cmap=[linspace(col1(1),col2(1),n1);linspace(col1(2),col2(2),n1);linspace(col1(3),col2(3),n1)];
cmap(:,end+1:end+n2)=[linspace(col2(1),col3(1),n2);linspace(col2(2),col3(2),n2);linspace(col2(3),col3(3),n2)];
cmap = cmap.';

% Plot
colormap(cmap), hold on, box on
p = scatter(X,Y,[],Z,'filled','DisplayName','Data3');
cb = colorbar;
cb.Limits = [18 23];
cb.Ticks = [18:1:23];

% Cosmetics
p.MarkerEdgeColor = 'k';
xlabel('X')
ylabel('Y')
cb.Label.String = 'Z';

解决方案

I think all that you're missing is a call to caxis to specify the minimum and maximum values to map the color range to:

caxis([18 23]);

Note that the following line...

cb.Limits = [18 23];

... only changes the tick limits displayed on the colorbar, but doesn't change anything about how the data is mapped to the color range. The caxis function is how you control that (in the above case, mapping the value of 18 to one end and the value of 23 to the other). By default, your code was mapping the minimum and maximum values in Z to the color range (20.5 and 23, respectively). When you then set the tick limits on the color bar to a larger range, it just filled it in with the last color in the color map, in this case red. That's why you see so much of it.

Bonus

Just because you might be interested, you could also use interpolation via the interp1 function to easily generate your color map like so:

cmap = interp1([1 0 0; 1 1 0; 0 1 0], linspace(1, 3, 41));

这篇关于在 MATLAB 中控制颜色条比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆