gpt4 book ai didi

Matlab 条形图 - 根据符号和大小填充不同颜色的条形图

转载 作者:太空宇宙 更新时间:2023-11-03 19:29:09 29 4
gpt4 key购买 nike

我正在尝试用不同颜色为条形图中的各个条形着色,比如蓝色表示正,红色表示负。我在互联网上找不到任何有用的东西。我在下面的代码中发现每个条形图都根据第一个条形图的值着色,而不是为每个条形图单独设置颜色:

c1=zeros(32,3);
c2=zeros(32,3);
for i=1:3
c1(:,i) = linspace(r(i),w(i),32);
c2(:,i) = linspace(w(i),b(i),32);
end
c= [c1(1:end-1,:);c2];
subplot(2,2,2)

bar(Numbers(end-7:end,1)), shading interp
caxis([-8 8]), colormap(c), colorbar

谢谢你的帮助

最佳答案

您可以使用 sign 将条形对象的属性更改为 -1/0/1 ,然后使用二元红/蓝颜色图:

y=rand(10,1)*3-1.5; % some data

hb=bar(y);
set(get(hb,'children'),'cdata', sign(y) );
colormap([1 0 0; 0 0 1]); % red & blue in rgb

bar plot with binary colors

您可以找到更多信息 here .

编辑:要让它变暗,您必须结合caxis 适本地设置cdata :

y=rand(10,1)*3-1.5; % some data
hb=bar(y);

% the colormap
Mc = 16;
Nc = Mc*2+1; % number of colors, uneven so there is a neutral middle
rgb = [1 0 0;0 0 1];
cmap = [linspace(rgb(1,1),rgb(2,1),Nc)' linspace(rgb(1,2),rgb(2,2),Nc)' linspace(rgb(1,3),rgb(2,3),Nc)' ];
colormap(cmap);

% cdata
c = y;
set(get(hb,'children'),'cdata', c);
cmax = max(abs(c));
caxis([-cmax cmax]);

bar plot with shaded colors

关于Matlab 条形图 - 根据符号和大小填充不同颜色的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13266352/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com