JerryYe的个人博客分享 http://blog.sciencenet.cn/u/JerryYe

博文

【MATLAB】一组含几个bar的柱状图的绘制以及errorbar的添加

已有 19617 次阅读 2018-3-11 19:02 |个人分类:Matlab|系统分类:科研笔记| 分组柱状图, bar, matlab, 可视化, errorbar, matlab

在数据统计分析时,往往会用到多个数据分组进行比较,这里介绍一种数据可视化方法。

实例:

% 数据

volume_mean=[0.73,0.45;

                        0.42,0.43;

                        0.70,0.42];                         

volume_std=[0.65,0.17;

                     0.35,0.14;

                     0.44,0.13];

%绘图                     

close all;figure;

h=bar(volume_mean);

set(h,'BarWidth',0.9);        % 柱状图的粗细

hold on;

set(h(1),'facecolor',[139 35 35]./255) % 第一列数据视图颜色

set(h(2),'facecolor','k')        % 第二列数据视图颜色


ngroups = size(volume_mean,1);

nbars = size(volume_mean,2);

groupwidth =min(0.8, nbars/(nbars+1.5));


% errorbar如果用不同颜色,可以利用colormap的颜色进行循环标记,这个例子没有用到colormap

%colormap(flipud([0 100/255 0; 220/255 20/255 60/255; 1 215/255 0; 0 0 1]));   % blue / red

% color=[0 100/255 0; 220/255 20/255 60/255; 1 215/255 0; 0 0 1];

hold on;

for i = 1:nbars

    x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);

    errorbar(x,volume_mean(:,i),volume_std(:,i),'o','color',[.5 .5 .5],'linewidth',2);

end


set(gca,'XTickLabel',{'2014','2015','2016'},'fontsize',14,'linewidth',2)

ylim([0 1.5])

set(gca,'ytick',0:0.5:1.5)

xylabel(gca,' ','Volume[Sv]')

legend('data1','data2','location','NorthEast')


以上实例可以参考使用。


errorbar的局部调整:

1.头部宽度调整

% Create errorbar

X = 0:pi/10:pi;

Y = sin(X) + 1;

E = std(Y) * ones(size(X));

ha = errorbar(X, Y, E);

% Width of the top and bottom lines of errorbar

xlength = 0.2;

% Make horizontal lines with 'line'

for k = 1:length(X)

 x = [X(k) - xlength, X(k) + xlength];

 y_h = [Y(k) + E(k), Y(k) + E(k)];

 line(x, y_h);

 y_b = [Y(k) - E(k), Y(k) - E(k)];

 line(x, y_b);

end

参考:https://www.mathworks.com/matlabcentral/answers/100333-how-do-i-change-the-width-of-the-horizontal-lines-at-top-and-bottom-of-error-bars-in-my-errorbar-plo




https://blog.sciencenet.cn/blog-2824237-1103377.html

上一篇:【MATLAB】调整fill填充颜色的透明度
下一篇:【悟】释放压力
收藏 IP: 222.173.84.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-12 16:54

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部