Matlab条形图 - 根据符号和大小用不同的颜色填充条形图 [英] Matlab Bar Graph - fill bars with different colours depending on sign and magnitude

查看:1665
本文介绍了Matlab条形图 - 根据符号和大小用不同的颜色填充条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用不同颜色的条形图对单个条形进行遮蔽,对于正面的红色表示负向的蓝色。我无法在互联网上找到任何有效的东西。我的代码如下,我发现每个酒吧都是按照第一个酒吧的颜色着色的,而不是每个酒吧的独立颜色:

  C1 =零(32,3); 
c2 =零(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(数字(end-7:end,1)),着色interp
caxis([ - 8 8]),色彩映射(c),colorbar

感谢您的帮助

解决方案

您可以使用。



编辑:为了让它阴影,你会有结合 cdata

  y = rand(10,1)* 3-1.5; %一些数据
hb = bar(y);

%colormap
Mc = 16;
Nc = Mc * 2 + 1; %颜色数量不均匀,因此存在中性中间
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]);


I am tring to shade the individual bars in a bar graph different colours, say blue for positive red for negative. I cannot find aything on the internet that works. I my code below I find every bar is coloured as per the value of the first bar, rather than separate colours for each bar:

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

Thanks for your help

解决方案

You can change the properties of the bar object to -1/0/1 with sign, and then use a binary red/blue colormap:

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

You can find futher info here.

EDIT: to get it shaded, you'll have to set the cdata appropriately in combination with caxis:

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]);

这篇关于Matlab条形图 - 根据符号和大小用不同的颜色填充条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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