Fed*_*ile 2 matlab customization colors colorbar colormap
我有以下代码:
[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]);
Run Code Online (Sandbox Code Playgroud)
我的目标是设置颜色栏的限制,以使颜色如下所示:
我相信我应该以某种方式更改CData变量,所以我使用了以下几行代码但未成功:
i = findobj(cbh,'type','image');
set(i,'cdata',[30 32 38]','YData',[30 55]);
Run Code Online (Sandbox Code Playgroud)
您的自定义(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]);
Run Code Online (Sandbox Code Playgroud)
导致: