声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 7238|回复: 7

[绘图技巧] 请问如何给多组bar图设置多种颜色?

[复制链接]
发表于 2009-7-21 21:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
例如
a=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]; bar(a);

出现的是一共4组,每一组四条bar图(每一组的最后两条因为数据很少,所以显得很低),可是都是蓝颜色。(我的问题里是分成四组数据的,这里为了举例简单)

我想每一组的四条bar图按红,绿,黑,蓝四种颜色区分排列,四组都是这样,在网上找了很久可都没有找到方法,我自己的想法是
bar(a(1,1:3:13),'r',a(1,2:3:14),'g',a(1,3:3:15),'k',a(1,4:3:16),'b');   可是不行

请大家帮帮我,谢谢了!
这个可以在程序里实现么,还是说只能在figuar加工中?

[ 本帖最后由 ChaChing 于 2009-7-21 22:36 编辑 ]
1.jpg
回复
分享到:

使用道具 举报

发表于 2009-7-21 22:35 | 显示全部楼层
第一次画这种图, 刚看看help下
a=1:16; aa=reshape(a,4,4)'; h=bar(aa);
set(h(1),'FaceColor','r'); set(h(2),'FaceColor','g');
set(h(3),'FaceColor','k'); set(h(4),'FaceColor','b');

还有bar画出的图, 好像预设即为不同颜色! (2006ra)

[ 本帖最后由 ChaChing 于 2009-7-21 22:38 编辑 ]
 楼主| 发表于 2009-7-22 16:47 | 显示全部楼层
真是非常感谢你的热心帮助,学到了新的东西,我试了一下,可是做出来的图形是被压缩了宽度 ,见test1.jpg
我自己试了两个方法
1。barwidth = 0.8;bar(aa)
      结果是没有变化,同test1.jpg

2。  aa=reshape(gesamt,6,4)'; h=bar(aa);
       set(h(1),'FaceColor','r','BarWidth'); set(h(2),'FaceColor','g','BarWidth');
       set(h(3),'FaceColor','k','BarWidth';  set(h(4),'FaceColor','b','BarWidth');
      结果见test2.jpg,matlab提示我set命令有错误, 宽度有了变化,可是颜色是我无法控制的,
      同时横坐标上的字样也不是我程序里作的,Sniffer 1, Sniffer 2,Sniffer 3,Sniffer 4
      而且我在程序里还有图例的命令,legend('Data','ACK','RTS','CTS');,也没有体现

可以麻烦你再帮我看看么,谢谢了

[ 本帖最后由 ChaChing 于 2009-7-22 20:38 编辑 ]
test1.jpg
test2.jpg
发表于 2009-7-22 20:22 | 显示全部楼层
二楼的方法试了试,正常。我的是matlabR2008a。

把set(h(1),'FaceColor','r','BarWidth');
……
改成
set(h(1),'FaceColor','r','BarWidth',3);
……
试试

[ 本帖最后由 friendchj 于 2009-7-22 20:27 编辑 ]

评分

1

查看全部评分

发表于 2009-7-22 20:49 | 显示全部楼层

回复 板凳 wj326_0 的帖子

刚刚又看下help! 请LZ亦仔细看下!
bar(...,width) sets the relative bar width and controls the separation of bars within a group. The default width is 0.8, so if you do not specify x, the bars within a group have a slight separation. If width is 1, the bars within a group touch one another.
还有LZ没给齐相关资讯, 个人无法给出建议!

[ 本帖最后由 ChaChing 于 2009-7-22 20:54 编辑 ]
发表于 2010-7-31 11:28 | 显示全部楼层
原帖由 friendchj 于 2009-7-22 20:22 发表
二楼的方法试了试,正常。我的是matlabR2008a。

把set(h(1),'FaceColor','r','BarWidth');
……
改成
set(h(1),'FaceColor','r','BarWidth',3);
……
试试

-------------------------------------------------------------
这个应该是正解---以前在matlab7.0里写过类似的函数引用

另外,好像MATLAB6.5不支持BarWidth属性,我原先用7.0版本写的程序,现在用6.5执行,会报错:
“Invalid patch property: 'BarWidth' ”
我的笔记本没有安装7.0,而且安装的6.5没有帮助,所以一时也搞不清二者对bar函数的支持具体区别有哪些。
发表于 2010-7-31 11:31 | 显示全部楼层
找到如下帮助:
http://www.mathworks.com/access/ ... lots/f10-19972.html
Overlaying Bar Graphs
In addition to grouping and stacking barseries, you can overlay several bars that share the same baseline and y-range by making each series of bars a different width and plotting the widest ones first. The following example shows how to accomplish this within an axes:

Define x and y data; it probably helps to make spacing of x values constant:

x=[1 3 5 7 9];
y1=[10 25 90 35 16];
K=0.5;

Plot Series 1 in blue, and set bar width to one-half an x unit:

bar1=bar(x, y1, 'FaceColor', 'b', 'EdgeColor', 'b');
set(bar1,'BarWidth',K);

Define Series 2, and plot it in red over the first series:

hold on;
y2=[7 38 31 50 41];
bar2=bar(x, y2, 'FaceColor', 'r', 'EdgeColor', 'r');

Set the width of the second series to half that of the first one:

set(bar2,'BarWidth',K/2);
hold off;
legend('series1','series2')
发表于 2010-7-31 22:51 | 显示全部楼层
...好像MATLAB6.5不支持BarWidth属性...

试了下v5.3, 的确不支持BarWidth属性!
但稍微修改下亦可
a=1:16; aa=reshape(a,4,4)'; h=bar(aa,3);
set(h(1),'FaceColor','r'); set(h(2),'FaceColor','g');
set(h(3),'FaceColor','k'); set(h(4),'FaceColor','b');
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-6-16 02:53 , Processed in 0.074633 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表