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

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

[复制链接]
(1)画竖直的errorbar, P1 Z" z  I8 h8 b4 p% }# j
例子:
. b* c3 Q2 Q; O0 L, R2 e4 |close all;figure;  _, h$ z! b" A
    h=bar(volume_month_mean./1e6);       %--volume_month_mean 为12×2的矩阵
2 g7 z$ ]& N8 T  V( h: p    set(h,'BarWidth',0.9);/ b2 X! \: f% y# R/ c9 _/ N) N
    hold on;
, ~6 n! Q, M/ K- H" p* R- i    set(h(1),'facecolor',[139 35 35]./255)    %--设置bar的颜色
- D# k% K4 b: {; S; ^( g    set(h(2),'facecolor','k')
; g; q6 k0 c7 L$ d# ?0 ~# N$ ]
' }; n% M2 Q- V8 X1 K! O5 }+ E0 {; J0 _. w- F' d+ H4 t
    ngroups = size(volume_month_mean,1);, g. p+ V7 k% C/ `) L, Z; ~7 a
    nbars = size(volume_month_mean,2);/ _, ^4 H; x1 {% V! H/ S( U1 M  P
    groupwidth =min(0.8, nbars/(nbars+1.5));
8 g9 Q9 l1 N' }0 y# A/ d9 Y1 y- t6 |; ?8 k

& Z1 L8 n+ ~# F( |    hold on;/ l+ b! l4 B8 {* K$ U- D
    for i = 1:nbars              %--画errorbar8 w" \! P1 z5 X2 Z0 ^
        x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);
& d2 {$ B* O% u! Z5 u8 |1 R% x        errorbar(x,volume_month_mean(:,i)/1e6,volume_month_std(:,i)/1e6,'o','color',[.5 .5 .5],'linewidth',2);
" G) W% B/ [+ F! ?    end
! J; L0 K( i4 c( M    # ]2 m6 t0 n4 Q/ }/ Q& k
    for n=1:12. K6 M" o5 s, l. K' y
        if n<=99 H8 Q: o1 O8 Y* H
            time_lab{n,1}=['0' num2str(n)];
7 V5 e1 U+ O; P" b        else/ j" M5 G3 O( T* D
            time_lab{n,1}=num2str(n);
1 M4 J, Z) {9 @9 Y( `/ [) }0 m, b        end1 K& i. f5 S, K/ K& @
    end
. p3 \5 W# [" P9 |/ g6 d2 zset(gca,'XTickLabel',time_lab,'fontsize',14,'linewidth',2)& _2 b" I$ f( E8 N: t
    ylim([-0.5 2.5])
6 n" M7 V7 G' w9 e5 K; u! _    xlim([0 13])) J/ Q. t5 J" u! ~( L/ u4 l0 F2 k
    set(gca,'ytick',-0.5:0.5:2.5)/ f1 E+ Y$ q  W, f7 g# x* v
   6 W% B' U8 D1 _" J6 R

5 t$ Y7 J$ ^6 e% K* W$ t( [( N6 ]4 B- v0 r* j( d
(2)画水平errorbar
- |9 c, J/ s7 W1 u( k: ^例子:
0 E! f- G4 t$ F: ~; d* |  cclose all;figure;& U( t6 B2 k/ V' K2 T/ n
    h=bar(volume_mean./1e6);           %--volume_mean是4*2的矩阵
; U/ o* s2 C5 p    set(h,'BarWidth',0.9);" F& p. F# V% t/ ?; w0 P; `
    hold on;( w6 ~0 v/ f" Y4 S  r' v
    set(h(1),'facecolor',[139 35 35]./255)
- C; J. P3 s* l' z# A8 Z    set(h(2),'facecolor','k'), u, V2 K% l; V" A2 C" `- E
& x2 n' J; ?7 g8 |8 v

- h/ {6 ]; d5 w& D& _' ~    ngroups = size(volume_mean,1);) H$ ]" C( {0 q2 {
    nbars = size(volume_mean,2);  h# k6 t! X7 q
    groupwidth =min(0.8, nbars/(nbars+1.5));
; o# e. ?" W* E2 Y8 y7 g4 V, n2 e" \3 y1 T  |( b8 M
$ q# |- g/ X% y, G4 U
    hold on;
" g5 \7 d5 [. @/ h' _, x    for i = 1:nbars
9 ]7 a9 S$ z4 S+ r        x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);
  i3 z: Z( }; o+ ^  i3 m+ R" C  k: R        errorbar(x,volume_mean(:,i)/1e6,volume_std(:,i)/1e6,'o','color',[.5 .5 .5],'linewidth',2);. C2 x0 o1 l0 i1 u# }! K
    end& Q$ c4 q; I. }" U( Z$ ^& u- X

6 K$ n, x8 c( \3 P( G$ w! @! s5 d# s0 n% O& D
    view(-90,90)
) \  t6 c8 `9 a! U% G; M    set(gca,'xdir','reverse','ydir','reverse','XTickLabel',{'Winter','Spring','Summer','Autumn'},'fontsize',14,'linewidth',2)
7 \- k5 ]2 ^* q& z/ x0 E2 x" \    ylim([-0.5 2.5])
7 w2 {6 @1 k- P& `: I$ j    set(gca,'ytick',-0.5:0.5:2.5)
$ H$ S: E' w3 j9 E. L! V    ye_xylabel(gca,' ','Volume[Sv]')+ o" m: B& P/ C$ Z
    legend('A','B','location','NorthEast')+ @+ N3 m! _$ e% i0 G/ B, e' f
   7 _* ~0 R# c' v7 T8 a5 s
( f8 ~9 h3 u& V8 A. c5 x; T* e* U
0 ~+ n4 C  Y8 @: f$ [
                    % ~. T2 C- ^' w4 B: v2 [8 f9 m
1 d% Z2 q  |0 ~" H
                                        转载本文请联系原作者获取授权,同时请注明本文来自叶瑞杰科学网博客。
回复

举报 使用道具

相关帖子

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