收藏本站 劰载中...网站公告 | 吾爱海洋论坛交流QQ群:835383472

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

[复制链接]
在数据统计分析时,往往会用到多个数据分组进行比较,这里介绍一种数据可视化方法。
6 J( L3 c& [) l实例:
0 U; _3 B+ n+ y% s  }' c/ E% 数据' t' x: R& s3 i
volume_mean=[0.73,0.45;' d6 o" z$ t9 _4 e$ s
                        0.42,0.43;# Z7 W" E( {  b4 k- O9 F
                        0.70,0.42];                        
7 ^4 @# j: Y: u5 a( lvolume_std=[0.65,0.17;
7 b% o1 P3 a1 @6 t3 @8 c6 h4 u/ g4 y                     0.35,0.14;
+ d, X8 u: r9 f, C                     0.44,0.13];% a. I7 [, M  p( l
%绘图                     
8 ]1 P5 n+ `/ z: E1 eclose all;figure;$ ^; b+ a* t8 {# E7 l# s
h=bar(volume_mean);* Q/ K6 l$ ^0 E8 t; G
set(h,'BarWidth',0.9);        % 柱状图的粗细1 l; l% [. V5 ^6 w( A0 v
hold on;
" W" ^! o3 c' S/ o* y5 v, C/ Vset(h(1),'facecolor',[139 35 35]./255) % 第一列数据视图颜色
0 ~4 `) T$ P# S+ O* ?3 q+ zset(h(2),'facecolor','k')        % 第二列数据视图颜色  O# l, y/ q: l" u# x
+ h9 a3 {7 t/ h6 h
' A; w$ p% U+ T2 M' Z
ngroups = size(volume_mean,1);
% R7 @. j5 w! z; H+ {( W; I& a2 Ynbars = size(volume_mean,2);
/ t# q8 Z2 O- y3 e% ]! hgroupwidth =min(0.8, nbars/(nbars+1.5));
) M) Z8 d4 C/ J. V4 m
6 B7 W8 y) V& O* k2 `6 u! ~# m8 x" @
  ]0 G! v# _* k8 j& ?* J% errorbar如果用不同颜色,可以利用colormap的颜色进行循环标记,这个例子没有用到colormap' J! P# u9 H0 W( q8 Q
%colormap(flipud([0 100/255 0; 220/255 20/255 60/255; 1 215/255 0; 0 0 1]));   % blue / red
: ^  f4 r% [8 Z8 @% color=[0 100/255 0; 220/255 20/255 60/255; 1 215/255 0; 0 0 1];
" s0 s2 j; k3 J/ w; \( I$ @/ d. O% Qhold on;
* U! l$ f' f1 {0 k; f4 t! }( Mfor i = 1:nbars" v  g. M# P+ n- ?4 h5 F
    x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);1 l: d; R  l4 h3 q1 ]7 C
    errorbar(x,volume_mean(:,i),volume_std(:,i),'o','color',[.5 .5 .5],'linewidth',2);
( |5 {4 l& h' v7 Kend
$ Z5 W$ ^- b% L2 ?7 |% |: j; Y" x
9 `4 ^3 M- F) W, c! [
set(gca,'XTickLabel',{'2014','2015','2016'},'fontsize',14,'linewidth',2)% `4 M0 F2 ^/ @. s* o( \
ylim([0 1.5])/ m7 o/ u: F! a
set(gca,'ytick',0:0.5:1.5)
! \3 K5 @& g0 b) _& dxylabel(gca,' ','Volume[Sv]')4 C  W, [% C  Z, q4 k3 [- \
legend('data1','data2','location','NorthEast')
: G8 H% f6 ^; m% s/ l% r- Z7 G% Y/ _3 s0 \8 d
0 V' M7 R  n0 Y+ Y' ]/ u% S
以上实例可以参考使用。
. j% f9 X) J- p2 w+ l9 D1 c& U' q) p: L1 m4 v8 Z& a& o  [2 V+ x) c
2 l$ \" H$ g* l  j. y
errorbar的局部调整:% U6 V1 W$ a% j8 e
1.头部宽度调整
3 g4 O- m$ I, p$ E  `( S. W% Create errorbar$ k1 x5 V+ u. u( H' B2 }
X = 0:pi/10:pi;
  C& }+ ?# ^# t2 ?% UY = sin(X) + 1;
$ m7 v/ v: b$ _E = std(Y) * ones(size(X));) A1 ^( D/ @, _4 ~( f
ha = errorbar(X, Y, E);- C; I1 R4 u# X; }2 F( B4 L8 U
% Width of the top and bottom lines of errorbar- [1 V. K5 {5 p, l; s  y: K
xlength = 0.2;
+ a# L% ]4 {( Q" A( j+ V, L  ?2 z1 i% Make horizontal lines with 'line'
; c4 C% ~) Y3 d- b4 v7 {for k = 1:length(X)
- n4 ?2 P% }( c1 S8 h x = [X(k) - xlength, X(k) + xlength];' D) _' j1 x9 R2 P4 s( }$ h
y_h = [Y(k) + E(k), Y(k) + E(k)];( N& l  ?* _: T! w2 N! l* d% Z9 f
line(x, y_h);
- k- `, d4 g# L, K- D/ b y_b = [Y(k) - E(k), Y(k) - E(k)];
7 B( W7 ^& I* t1 D line(x, y_b);
: W+ Z- [" T1 g( m8 h9 Zend6 f" `  h  R! {9 A) f
参考:www.52ocean.cn 6 B3 w3 Q: v0 A+ F% V- r

; L! D1 I, W/ v: d) b* d0 f- S6 y% ~, ~/ L
                    
# f- z; y7 h7 C: x( D( d, x* N: Q' i( U# F
                                        转载本文请联系原作者获取授权,同时请注明本文来自叶瑞杰科学网博客。
回复

举报 使用道具

相关帖子

全部回帖
暂无回帖,快来参与回复吧
懒得打字?点击右侧快捷回复 【吾爱海洋论坛发文有奖】
您需要登录后才可以回帖 登录 | 立即注册
黄金品质
活跃在2021-12-8
快速回复 返回顶部 返回列表