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

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

[复制链接]
(1)画竖直的errorbar
: L+ h1 N- W( M8 s/ T4 z3 `例子:! l5 x8 y3 V1 j9 N
close all;figure;
+ L3 D9 w1 Z: {3 z) T7 g! i) y+ V    h=bar(volume_month_mean./1e6);       %--volume_month_mean 为12×2的矩阵: ^; H; J3 u" u
    set(h,'BarWidth',0.9);, y/ r4 v4 j$ F; {1 K- {
    hold on;
" y. M* U0 p3 j4 O" W2 \# c% R# m    set(h(1),'facecolor',[139 35 35]./255)    %--设置bar的颜色: p6 g: Y! H/ k
    set(h(2),'facecolor','k')
$ Q% H& n+ U1 ~2 Z$ ]* [; Z- p7 o. j. E3 T- m( h, K! l% F) s. N
& `$ Q$ M: h1 @& W- g8 D; v
    ngroups = size(volume_month_mean,1);8 x8 f, ^( d3 }( n
    nbars = size(volume_month_mean,2);) U2 h/ v1 T8 _% z& P6 \! L
    groupwidth =min(0.8, nbars/(nbars+1.5));: Q, ^1 ]# m: Y/ r$ k5 L. e
" P  o" j8 l" Y$ F9 y' b
- s9 [( {) V: w8 {
    hold on;
( y0 M: X$ M. H7 K  T8 \2 e    for i = 1:nbars              %--画errorbar7 H7 S% S# L/ p5 o! Q
        x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);
# R) c9 \7 X5 U7 ]+ A! d! g        errorbar(x,volume_month_mean(:,i)/1e6,volume_month_std(:,i)/1e6,'o','color',[.5 .5 .5],'linewidth',2);. J" \, D) k1 Z: Q5 J! I% G
    end* l/ \( d& D! ~+ x& Z. |- A+ n
    1 s1 @/ M4 Z% m6 B
    for n=1:12" b9 E+ v9 V3 W3 l* q
        if n<=9
% t8 Q, s; [6 Y            time_lab{n,1}=['0' num2str(n)];
' o7 g' H1 N% L) K+ G# J5 t, {        else
* T- s- l2 b  ~4 v8 k* K0 ]. L            time_lab{n,1}=num2str(n);: G, P  q6 c+ N
        end  I$ O! ]) T, k( N
    end
- _+ }% c; e( ]! y0 Rset(gca,'XTickLabel',time_lab,'fontsize',14,'linewidth',2)
; \. r# V. d8 r0 X/ ~" Q- |    ylim([-0.5 2.5])0 E9 g, V7 p$ D$ Z
    xlim([0 13])
( x  E9 g/ B4 n    set(gca,'ytick',-0.5:0.5:2.5)
4 k+ o, a' ~$ Q3 d8 t   
6 g# ]0 z: v8 q! I9 b/ c+ w
" |9 S! K) l! ^5 G# T- h2 y: `0 X! k( ?; e8 U4 [+ S0 b9 d8 V+ s
(2)画水平errorbar5 f# }$ @1 f' e" }
例子:
; X6 [1 c; t$ vclose all;figure;
4 U5 L' Z. S) ]  h: F( P    h=bar(volume_mean./1e6);           %--volume_mean是4*2的矩阵
* U1 ~6 z8 Y/ Y    set(h,'BarWidth',0.9);  ]; Z' d5 u8 [; e) `7 f+ I/ t/ G) j
    hold on;
6 n$ c# t% A$ s) u4 Z$ u, t9 A* ?    set(h(1),'facecolor',[139 35 35]./255): a* X5 `8 N5 ^2 Q; \
    set(h(2),'facecolor','k')# |* }) f: j/ {% X, \

: x$ G( `1 j& W- B: u, A" s9 i9 G* J0 x% m! K
    ngroups = size(volume_mean,1);. b9 M" i) {. a3 B& R% ~  i
    nbars = size(volume_mean,2);" F+ ]* N+ }( Q' U: `2 E" o
    groupwidth =min(0.8, nbars/(nbars+1.5));4 M& r. W8 {9 @# p! m% W

& G3 P' ~8 }6 ~+ A, n* l" @6 f
! n( ^$ r; Y, G- \$ v    hold on;! n; D9 r! S3 u: {: b4 x' g
    for i = 1:nbars% H- ~! b1 d# ?
        x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);. \; H( d, U; m4 S) g. D
        errorbar(x,volume_mean(:,i)/1e6,volume_std(:,i)/1e6,'o','color',[.5 .5 .5],'linewidth',2);
' |8 D4 r7 Y9 h+ y% |# w3 j' P3 Y    end
# d1 I% b# u! n% S7 A% n
/ G8 [; K9 F; L4 F5 \
3 {. {+ F& e( s    view(-90,90)9 N8 f/ J2 @- T$ a
    set(gca,'xdir','reverse','ydir','reverse','XTickLabel',{'Winter','Spring','Summer','Autumn'},'fontsize',14,'linewidth',2)
) r9 D8 d- g5 q    ylim([-0.5 2.5])2 d  D9 r; e# N0 N7 q! i* l- [
    set(gca,'ytick',-0.5:0.5:2.5)
1 c: v5 ~1 V- f+ S3 _1 i' m    ye_xylabel(gca,' ','Volume[Sv]')
! z: ?0 @, \" ~  F. e    legend('A','B','location','NorthEast')
* g% G' r1 N3 D* [   
- W  r3 U. P# h* M6 j! ^4 j$ P+ A. j4 H0 T: O, G! p/ @
: z* [( f. k" z! Q
                    
7 i* \$ ]9 x; W7 ]# l1 [# _& F. y: q3 i* [6 [$ Y
                                        转载本文请联系原作者获取授权,同时请注明本文来自叶瑞杰科学网博客。
回复

举报 使用道具

相关帖子

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