在数据统计分析时,往往会用到多个数据分组进行比较,这里介绍一种数据可视化方法。
/ g& o9 n: q3 w. x J实例:: P) Z$ D5 V( [, \+ Y$ }4 a5 h
% 数据6 q; [, K' J/ E3 i+ @# s
volume_mean=[0.73,0.45;
& g3 t* h- g( A$ d. K! S 0.42,0.43;' e! @8 P; }+ Y( c
0.70,0.42];
# C9 [( f4 b7 n0 ^$ S# d" G4 E% g% hvolume_std=[0.65,0.17;
) H+ s9 t2 ~5 Y0 X8 @# l4 \; G 0.35,0.14;
0 y7 \5 y0 S9 i& [1 y% v 0.44,0.13];$ z$ \; ~, u- {9 S3 F% M
%绘图 , K3 B* d2 B: U1 N' T& `4 q
close all;figure;
- j7 j+ y! h( W& Y( E1 Y6 Wh=bar(volume_mean);
8 M3 r9 m3 R3 c" m5 F& H3 s. rset(h,'BarWidth',0.9); % 柱状图的粗细: l9 }8 ^1 L) P
hold on;8 a- y; v5 ]) n
set(h(1),'facecolor',[139 35 35]./255) % 第一列数据视图颜色. }. g) }5 W4 _3 L
set(h(2),'facecolor','k') % 第二列数据视图颜色' y! y, p/ P; c8 V2 o* \" A
" T1 z7 N9 [ Q) q! H, }1 w% `, k
ngroups = size(volume_mean,1);8 i( y/ m2 H; g/ X
nbars = size(volume_mean,2);
. M$ T' K5 ^) [4 i1 { Z$ h" }groupwidth =min(0.8, nbars/(nbars+1.5));
* X" L+ X' y0 ]7 w+ ^% C6 ^) D5 q8 T; u
' {; s8 q% D$ x( T1 @1 ?% errorbar如果用不同颜色,可以利用colormap的颜色进行循环标记,这个例子没有用到colormap
2 \; l( M5 P" `2 F! |%colormap(flipud([0 100/255 0; 220/255 20/255 60/255; 1 215/255 0; 0 0 1])); % blue / red
7 R$ a4 x- [' S1 J) T% color=[0 100/255 0; 220/255 20/255 60/255; 1 215/255 0; 0 0 1];
0 J( c0 r' r( chold on;5 d5 A. | }0 i1 J( o
for i = 1:nbars; u- o0 x d/ V6 p S) ~9 m& ?
x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);( G' Z H8 @* e) ?* h z8 j" z2 v
errorbar(x,volume_mean(:,i),volume_std(:,i),'o','color',[.5 .5 .5],'linewidth',2);4 u9 T+ \& u, k0 E4 S
end
7 |8 n9 F7 `7 v/ z1 J2 K
" h# k3 Y3 {, q ~0 m2 K$ A* J# p1 w' `; z5 O! X
set(gca,'XTickLabel',{'2014','2015','2016'},'fontsize',14,'linewidth',2)
, @5 H5 ~9 l8 x5 k3 n3 m2 hylim([0 1.5])
2 V+ s$ \5 J- Xset(gca,'ytick',0:0.5:1.5)
0 v) q& v3 S0 e8 Y" D b; axylabel(gca,' ','Volume[Sv]')
5 p% O S$ P/ H; |* y1 p mlegend('data1','data2','location','NorthEast')' s' P3 W- z6 q+ E6 E0 b
( V, @- F. M2 p T$ {
T. c: x' w% V w) G以上实例可以参考使用。
! B9 h+ t; j3 F0 G8 U H, D: t% X) ?# s' F) q$ h
5 E+ j# P# d( G8 f
errorbar的局部调整:, R) i* M) @6 v# m; `+ I# y" s1 P
1.头部宽度调整
- H, Z k# l! F2 M' u6 X; M! ^% Create errorbar0 S% q6 m5 k$ j% ?7 n5 B; M! [
X = 0:pi/10:pi;# k9 Q6 S7 G7 f* e" G r
Y = sin(X) + 1;
! `7 \% J j+ j! f. }: L+ Y8 a' _E = std(Y) * ones(size(X));, u9 ~" B7 C: o* A. v* S$ S0 |
ha = errorbar(X, Y, E);4 d. R, _( S+ x
% Width of the top and bottom lines of errorbar2 M6 q* E) D. _& w Q
xlength = 0.2;
/ @7 C( g0 q* O" I" g9 w1 c% Make horizontal lines with 'line'
& k3 q8 E& x/ g4 nfor k = 1:length(X)! m8 g/ r0 H) |/ C# H* Y: ]( J. X8 |8 n
x = [X(k) - xlength, X(k) + xlength];7 U" R1 g$ D. s/ Z( @8 V( M
y_h = [Y(k) + E(k), Y(k) + E(k)];
! I: D+ p% C5 b% i3 d( } line(x, y_h);' E( M0 D* s0 i- R1 }
y_b = [Y(k) - E(k), Y(k) - E(k)];+ o b8 q r; |! Z% a
line(x, y_b);! t5 Y4 w7 f2 X: z& ^( }4 L
end
4 {: P+ g0 f2 z ]参考:www.52ocean.cn
* \4 U8 m) s/ }4 W8 s% R1 N& |2 j; r
; H+ U/ {) a6 u# n7 i, t) O: q
6 P% }; x2 C( i; F& s- Z H# e5 N' n5 z3 e. k# X: K$ ~8 [
转载本文请联系原作者获取授权,同时请注明本文来自叶瑞杰科学网博客。 |