如何控制颜色栏颜色范围?

问题描述 投票:1回答:1

我有以下代码:

[X,Y,Z] = peaks(30);
crange = 1.5;

[maxval dummy] = max(Z(:));
[minval dummy] = min(Z(:));

% green, yellow, red
cmap = [0 1 0; 1 1 0; 1 0 0];  

figure
colormap(cmap); 
surf(X,Y,Z);
caxis([30 55]);
cbh=colorbar;
set(cbh,'Ytick',[30 32 38 55]);

enter image description here

我的目标设置颜色条的限制,以使颜色像这样:

  • 绿色从30到32
  • 黄色从32到38
  • 从38到55的红色

我相信我应该以某种方式更改CData变量,所以我使用这些代码行没有成功:

i = findobj(cbh,'type','image');
set(i,'cdata',[30 32 38]','YData',[30 55]);
matlab colors customization colorbar colormap
1个回答
3
投票

您的自定义颜色条由(32-30 = ) 2 + (38-32 = ) 6 + (55-38 = ) 17 = 25个“单位”颜色组成。因此,一个简单的技巧就是将每种颜色复制所需数量的“单位”:

function q58097577
[X,Y,Z] = peaks(30); Z = (Z - min(Z(:)))*5;

% green, yellow, red
nG = 32-30; nY = 38-32; nR = 55-38;
cmap = [ repmat([0 1 0], [nG 1]); repmat([1 1 0], [nY,1]); repmat([1 0 0], [nR,1]) ];  

figure()
colormap(cmap); 
surf(X,Y,Z);
caxis([30 55]);
cbh=colorbar;
set(cbh,'Ytick',[30 32 38 55]);

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.