【MATLAB】如何画带errorbar的直方图

[复制链接]
(1)画竖直的errorbar# y2 q' K& [, ^" F" u) [4 L
例子:
1 b# q% @% k  k! D5 ?close all;figure;
, U9 p5 t' H$ t( p6 v  I: S% D    h=bar(volume_month_mean./1e6);       %--volume_month_mean 为12×2的矩阵6 e& e) {" e3 p4 S# [/ h0 V0 S
    set(h,'BarWidth',0.9);
/ E) K: T$ `# B7 N7 u1 J# t    hold on;
" W' O3 _8 |0 E2 L# \    set(h(1),'facecolor',[139 35 35]./255)    %--设置bar的颜色3 ]8 f, p/ Y" f3 L1 J. l, l9 w
    set(h(2),'facecolor','k')! b* D/ n4 n; u9 m$ `( A$ q+ Q# x
# d+ A. ^+ s  m! i5 t4 T2 R
, a# t. t- J+ c6 W2 W3 r
    ngroups = size(volume_month_mean,1);
# p) i7 l) p" g' H  q    nbars = size(volume_month_mean,2);7 I2 \/ k1 j+ g9 d2 u! V+ F
    groupwidth =min(0.8, nbars/(nbars+1.5));
3 |( r% w) a- D
0 L& m7 @! S& {* a/ h( }+ x3 y# L! j; \$ `9 o9 X0 [; e1 K/ l
    hold on;) M9 U; {7 t0 Q" ?" t
    for i = 1:nbars              %--画errorbar4 a! m$ v3 F# D9 [, X! h
        x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);& d# N& v# f, X" c" b* ~& M
        errorbar(x,volume_month_mean(:,i)/1e6,volume_month_std(:,i)/1e6,'o','color',[.5 .5 .5],'linewidth',2);0 G5 I( r1 U9 p& w( W, @
    end
+ k; J4 S/ j7 t- l3 [% t5 p   
$ O9 |, w% }& l/ f3 h9 d    for n=1:12. E8 B% L9 [2 J5 d8 [1 n: D
        if n<=9
" c  y; x$ ~& y4 h* m9 R            time_lab{n,1}=['0' num2str(n)];3 W' m) q: F  X
        else
6 I- `: s4 e; R  P! V( W- b2 y            time_lab{n,1}=num2str(n);
9 g$ g6 [* N0 {0 ~* K: y- d' r        end0 C/ \5 m/ F8 V7 t: N: e
    end; s9 `8 E0 G, u5 T
set(gca,'XTickLabel',time_lab,'fontsize',14,'linewidth',2)& z' k' O! G1 k$ l
    ylim([-0.5 2.5])
2 |  m- e+ b. \& A. X( q4 O    xlim([0 13])! S! f: l6 z! p" t
    set(gca,'ytick',-0.5:0.5:2.5)+ V: Z& \2 t" V9 n+ z
   
3 @0 y; x7 s2 n2 |6 N& h1 c8 U) j6 ^8 I6 k& R6 m. F
2 I; k3 F+ v, \% R
(2)画水平errorbar: Z+ {% H" d1 q9 }, k: q. k
例子:- `5 A8 q& m8 `  _
close all;figure;
9 `" L" @1 X& b+ j" _    h=bar(volume_mean./1e6);           %--volume_mean是4*2的矩阵0 t: K( r4 _4 f% P
    set(h,'BarWidth',0.9);
2 u% v$ ^0 M, f7 v' _    hold on;
& \8 T* z/ ]) x/ i3 W/ `' C% _    set(h(1),'facecolor',[139 35 35]./255)2 c+ A$ q" f: g: c
    set(h(2),'facecolor','k')
1 @+ [  r4 x' P2 W  J6 G. x) ?
5 c5 l. x7 N5 ~7 U$ L0 d" C- C
    ngroups = size(volume_mean,1);
1 X0 I+ {5 f  k4 D' X  d    nbars = size(volume_mean,2);, r* {( i. l# K( D* C4 h( Z
    groupwidth =min(0.8, nbars/(nbars+1.5));
9 T% K6 v" f( n- N4 g
  h& R( R* P1 C$ v% [: H/ n2 \4 c& C, p0 \( a3 w$ r$ a7 f+ J
    hold on;: g6 K) s0 D2 v! ^8 J' r
    for i = 1:nbars0 L7 n: c3 d2 Q* w& j. d% d9 _
        x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);
4 M" x( u4 i0 C: v. H  c        errorbar(x,volume_mean(:,i)/1e6,volume_std(:,i)/1e6,'o','color',[.5 .5 .5],'linewidth',2);5 o1 l) q! y0 R1 r. D
    end$ y8 ^! u4 ^. ?' h( }$ z6 D1 {* F
% w6 U# T5 [, S* T0 h
5 l) l9 \1 h' L
    view(-90,90)
& N; C0 x% Q2 `0 P, `7 h- `    set(gca,'xdir','reverse','ydir','reverse','XTickLabel',{'Winter','Spring','Summer','Autumn'},'fontsize',14,'linewidth',2)
' s0 b1 m4 t2 I; K    ylim([-0.5 2.5])
4 E$ O. T$ z7 G! ]4 P" G6 C    set(gca,'ytick',-0.5:0.5:2.5)
4 T7 {: Z3 {, |  L: _* |/ B; j. W    ye_xylabel(gca,' ','Volume[Sv]')6 ]( H1 Y1 L0 t% g3 P# B; r
    legend('A','B','location','NorthEast')
( `( U; N: }. s& Z9 k7 l   
. m, C7 l5 }# b/ W( g1 }6 V2 l# p. D9 i- M! ]# ?$ e6 W2 ]; g

) W% ^# R+ L+ B9 a                    
3 \) x$ W* ?2 W  ], T( Y$ _6 }0 B0 x1 @! Z! \+ c
                                        转载本文请联系原作者获取授权,同时请注明本文来自叶瑞杰科学网博客。
回复

举报 使用道具

相关帖子

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