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

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

[复制链接]
(1)画竖直的errorbar
( F; z: f8 a" M) t$ p例子:
9 X' v8 m, k1 zclose all;figure;& R, W& r4 D; Y6 H
    h=bar(volume_month_mean./1e6);       %--volume_month_mean 为12×2的矩阵- \1 G2 O7 ~" f* C/ i1 l
    set(h,'BarWidth',0.9);" t2 ~3 ^3 d: \9 C
    hold on;
( b0 ~: v* p3 m2 p$ {    set(h(1),'facecolor',[139 35 35]./255)    %--设置bar的颜色. i+ b* d3 }% A( k
    set(h(2),'facecolor','k')
/ X% v9 a9 W) O! l) y4 X+ y9 @! c4 L1 O8 \+ i0 Y% J8 b  G
" {2 k  H5 O5 X1 E, G4 I: `5 ^
    ngroups = size(volume_month_mean,1);
- L! }3 S% x9 c% c& i: W    nbars = size(volume_month_mean,2);
% M* w2 b% C) `. b, X    groupwidth =min(0.8, nbars/(nbars+1.5));
( ]& G8 I4 w2 r6 u$ K2 C# ?) N* p& R1 S+ M$ }' w/ }/ J
- f, [  Y) m- E& S" `
    hold on;
5 R% G4 r1 o) n& q% {    for i = 1:nbars              %--画errorbar
/ S' d/ u% {8 a& S/ Q7 V2 Y        x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);9 o& q" v# k" }, \% X
        errorbar(x,volume_month_mean(:,i)/1e6,volume_month_std(:,i)/1e6,'o','color',[.5 .5 .5],'linewidth',2);
" h* P+ O+ ?0 a0 C% Y' w6 f    end: J" O9 P6 x* j7 i' P: J
   
# u# G) a- Y: O$ A- T2 u    for n=1:121 L" a; _/ B1 u" E3 L0 i
        if n<=90 Z* k9 D' K  B8 U6 W
            time_lab{n,1}=['0' num2str(n)];7 A" H0 b' _2 A/ O' H* P% X
        else
* K8 C9 a+ R! G: H$ s3 ]6 r! C) j            time_lab{n,1}=num2str(n);& i0 z/ T: Q( K* R
        end
& u8 [& ], @; q& \1 J    end
, R" c# E) M+ L5 c2 kset(gca,'XTickLabel',time_lab,'fontsize',14,'linewidth',2)
4 D2 ^; e( U) Z- b  D    ylim([-0.5 2.5])9 ~0 X5 d/ W4 e: W, |+ Y4 e5 F
    xlim([0 13])8 t+ w( w$ w' k4 g; H
    set(gca,'ytick',-0.5:0.5:2.5)
: \! g: J; B9 t, l   
- u3 v1 ^) K% R1 n3 g  B6 E% G! h! ?7 J) f: P

! i9 ~6 e! N7 G% G7 A(2)画水平errorbar
: A# m; s( x9 K& T. g1 s3 m$ D例子:
( U5 C! V7 y" j/ [2 ?- b2 L3 ?close all;figure;
1 m5 O2 o. l4 R    h=bar(volume_mean./1e6);           %--volume_mean是4*2的矩阵- m6 \7 `5 u2 ?/ X4 F
    set(h,'BarWidth',0.9);
! R% L0 W/ }2 b8 V8 s! w    hold on;
8 h9 ^! V1 h8 X    set(h(1),'facecolor',[139 35 35]./255)
3 S) X$ S5 x/ ~    set(h(2),'facecolor','k')) P5 P. h" K8 W( x7 Q% N
7 j& R; h) L* V/ {" n6 e. _
! j0 k% l! x1 ?0 y( V7 [2 c
    ngroups = size(volume_mean,1);9 P/ S8 f) h" i5 `* ~
    nbars = size(volume_mean,2);& G3 L" A% \' Q/ _
    groupwidth =min(0.8, nbars/(nbars+1.5));. u) E) T3 o5 K4 q3 s/ l
8 {& V7 ]3 o- ]& r6 ^5 U1 z
+ U5 u; o( S3 V  L) q8 n
    hold on;
* R# b# ]$ N' n( I* f! i0 c2 d    for i = 1:nbars6 t/ B' x5 ~6 i* q0 _/ B& k
        x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);0 C- C1 a) `- M5 P
        errorbar(x,volume_mean(:,i)/1e6,volume_std(:,i)/1e6,'o','color',[.5 .5 .5],'linewidth',2);
. X: w* V+ r+ o/ w' l    end
5 f. ?3 y' t" O8 {7 \3 T: m! {7 T! ?
: t) q) D  R. W2 `( g" n
& `: F3 K0 q$ Q    view(-90,90)% {- {0 r1 u% p( O7 u; t
    set(gca,'xdir','reverse','ydir','reverse','XTickLabel',{'Winter','Spring','Summer','Autumn'},'fontsize',14,'linewidth',2)
/ I: M) z$ f4 H: a7 x! b    ylim([-0.5 2.5])2 b1 _! U+ n: B! r& B
    set(gca,'ytick',-0.5:0.5:2.5)/ ]7 @  D# ?% j" D1 o' `9 J( I
    ye_xylabel(gca,' ','Volume[Sv]'): |* H5 v9 x$ d: q3 a$ E8 s- v
    legend('A','B','location','NorthEast')
- {. D( d. i: c% x) T' @: @- Y% I  {7 g   
; M, C+ g9 I; @9 b( ?4 s, j' C9 R2 ?) i) P1 ?
" i) E  B1 U7 i+ \+ e8 u% ?
                    $ n) ^; b' O8 q$ Z7 e
- i; q0 l6 W; C1 J5 n8 v
                                        转载本文请联系原作者获取授权,同时请注明本文来自叶瑞杰科学网博客。
回复

举报 使用道具

相关帖子

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