在数据统计分析时,往往会用到多个数据分组进行比较,这里介绍一种数据可视化方法。3 f' ~' B# x) O" X# [5 T
实例:
& v* |6 s7 q5 P' \: `# O% 数据
0 M9 h* D9 J$ K/ ?" E1 Uvolume_mean=[0.73,0.45;* F. X, p( N' s7 [; I/ X
0.42,0.43;' K; Y9 [. N8 U: \8 J
0.70,0.42];
# b; W0 W2 G! v' Z( P, Hvolume_std=[0.65,0.17;& ^, k3 M P3 W, X( b
0.35,0.14;
1 t" Y% u0 h& y. J3 S 0.44,0.13];* p* h3 ?2 {: d9 N! Z [* }3 }' q
%绘图
_* |2 a+ |; k; `close all;figure;: I5 o. P* Y6 i, Q( n! J
h=bar(volume_mean);1 [% p: u, J- N
set(h,'BarWidth',0.9); % 柱状图的粗细
: z2 q/ h+ |% U0 \hold on;
, p9 B9 v5 o* Dset(h(1),'facecolor',[139 35 35]./255) % 第一列数据视图颜色
! V5 `2 n7 |2 O# wset(h(2),'facecolor','k') % 第二列数据视图颜色9 z8 ?! ~; \+ _' A f7 S7 {6 g
% w, p; Z; B" S1 ^" U2 b- L% \
$ L& Q4 x7 E2 ^& k
ngroups = size(volume_mean,1);
9 o' k; t E0 M0 F( ^% S" knbars = size(volume_mean,2);
& `: y1 [' N" }1 ggroupwidth =min(0.8, nbars/(nbars+1.5)); {1 h( ?* |8 a3 A+ q1 K C/ C
/ P5 e+ [5 `7 ]1 p& T6 z
9 f( {$ K2 B8 n4 @6 [0 C% errorbar如果用不同颜色,可以利用colormap的颜色进行循环标记,这个例子没有用到colormap
- A" N: S* b2 f6 I' r& l" ?/ B7 Z%colormap(flipud([0 100/255 0; 220/255 20/255 60/255; 1 215/255 0; 0 0 1])); % blue / red" I' X, W1 C, ^2 \' b$ Q0 Q5 L
% color=[0 100/255 0; 220/255 20/255 60/255; 1 215/255 0; 0 0 1];
* d+ y+ e$ g% \hold on;+ H/ j9 _( S. ^$ n
for i = 1:nbars
) b; s6 u; d, |& Z/ ^ X x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);
' T. S. u) M- x5 g) h errorbar(x,volume_mean(:,i),volume_std(:,i),'o','color',[.5 .5 .5],'linewidth',2);
9 y+ I; B2 m) n/ O: o5 Wend
& w5 K# o! p: Q( @, @
, R) F2 X4 c9 {( M
% _" B4 Y* g8 [/ O) m' Gset(gca,'XTickLabel',{'2014','2015','2016'},'fontsize',14,'linewidth',2)2 H6 I& F+ f1 ~7 n) P
ylim([0 1.5])
# O0 c- ^ D0 u+ lset(gca,'ytick',0:0.5:1.5)
' F$ s. ~! Y; l2 b4 }$ ^" G5 _xylabel(gca,' ','Volume[Sv]')
, e4 M8 ~1 O% }) @5 a! p; u9 [% slegend('data1','data2','location','NorthEast')
2 }, g6 J' [. i- }
6 A9 R' e# J, b- Q+ k
. d6 ~6 h/ d0 ^/ b: \+ j以上实例可以参考使用。$ ~4 W8 B( o. C' R
: H" E# m7 M; F" _0 {
5 t$ G6 S- H' t5 j ?
errorbar的局部调整: P4 j# D0 }& ? l4 y) T Z+ z( d
1.头部宽度调整
# M; U: J( ~! V9 ^% Create errorbar
( y" A/ F$ C4 G4 ~( Q6 WX = 0:pi/10:pi;$ u; a( A' W' O: J; `/ F, [
Y = sin(X) + 1;
0 o$ y5 a$ O; x5 JE = std(Y) * ones(size(X));
; z+ V) ~5 p% u% q# z7 hha = errorbar(X, Y, E);# u2 n; N: m0 S3 D: j- ^
% Width of the top and bottom lines of errorbar
) ? [! @& C" |0 m% Lxlength = 0.2;; E7 m8 R! V6 P x; q
% Make horizontal lines with 'line'
. }" a( W2 g9 P6 N( `for k = 1:length(X)
; K, |: J% S) }$ |2 Z x = [X(k) - xlength, X(k) + xlength];
- o, E6 m+ _3 B- }$ x+ \% R y_h = [Y(k) + E(k), Y(k) + E(k)];
5 q, J* j! A+ U8 Z8 ]2 E/ s line(x, y_h);% {2 r9 H* @2 r8 j9 b# F
y_b = [Y(k) - E(k), Y(k) - E(k)];
" ?* v' a$ K7 d* g) k+ R, e line(x, y_b);
+ J2 \' j) J+ N) i _8 r2 I1 }end
; o+ T X9 Y7 F5 @/ ^+ Z参考:www.52ocean.cn ' U9 P* M7 E% j; ]+ W
, d, M7 ^: v. ]' N3 z
1 c8 L% v1 v+ Y* g
6 _. I% N( ]) N8 F. L) c8 {+ W
3 l" [4 Q$ ]1 t% p 转载本文请联系原作者获取授权,同时请注明本文来自叶瑞杰科学网博客。 |