我在matlab中有2d线图,每条线根据一个值着色。我想添加一个颜色条,显示与这些值对应的颜色。
我得到了一个根据我想要的值绘制线条的解决方案,但是我无法弄清楚是否正确获得了颜色条。我一直在寻找这个,但我被卡住了。
简约示例:
% Generate 10 lines of 10 points
x = normrnd(0,1,10,10);
% The corresponding values are
% Note that these do not have to linearly spaced in real code
z = [0,0.05,0.1,0.11,0.12,0.2,0.4,0.45,0.8,0.9];
% Define colormatrix
COL = [0.996078431372549 0.878431372549020 0.823529411764706;...
0.937254901960784 0.231372549019608 0.172549019607843;...
0.403921568627451 0 0.0509803921568627];
% Interpolate the COL matrix to get colors for the data
TRUECOL = interp1(linspace(0,1,3),COL,z,'pchip');
% Set the axis coloring qnd plot the data
set(gcf,'DefaultAxesColorOrder',TRUECOL);
plot(x);
colormap(TRUECOL);
colorbar
然后我更改了色彩图并绘制了colobar,但是颜色条中的颜色与z值不对应。有没有办法告诉matlab哪种颜色对应哪个值?看看colorbar编辑器,我看到CData必须与它有关,但是我找不到一种方法来指定CData应该是z。
答案 0 :(得分:2)
我的理解是您希望颜色条上的标签从0到1,而不是0到11.要解决此问题,请使用此caxis
命令。要在颜色条中获得更精细的颜色渐变,您需要更精细地插入颜色图。试试这个:
colormap(interp1(linspace(0,1,size(COL,1)), COL, linspace(0,1,100)));
caxis([0,1]);
colorbar