在数据统计分析时,往往会用到多个数据分组进行比较,这里介绍一种数据可视化方法。- b- E P! [ m9 I
实例:
7 g3 A/ G( j4 W, L, Q% 数据" _& L2 m' S( T
volume_mean=[0.73,0.45;% T- u/ J' t; W& Y, J% P! L6 o! N& ~
0.42,0.43;
& V+ i; K) y) d- d 0.70,0.42]; " N1 c7 {, e$ N! v
volume_std=[0.65,0.17;$ q% G1 o: Y# D* X
0.35,0.14;
; j& y. T4 ~" ^7 A) {5 n( o 0.44,0.13];! ?& W* g7 @) U
%绘图 ( i8 r: n; G( A) ?3 k
close all;figure;
, [3 z2 U( P! Rh=bar(volume_mean);
- w# J5 e m0 i0 B2 h' W6 Jset(h,'BarWidth',0.9); % 柱状图的粗细
3 T/ F9 W$ i# U; L3 s Zhold on;+ \5 M. c; W# `5 m) y
set(h(1),'facecolor',[139 35 35]./255) % 第一列数据视图颜色8 j& U6 ^% |( P* B: C* O% \! Q2 @9 |
set(h(2),'facecolor','k') % 第二列数据视图颜色% {& d7 V! E- b( U" d3 m
: p% s- g# R( |/ n6 f+ K8 a. @* [
* b) i C9 O) u) X) s$ Tngroups = size(volume_mean,1);
, C" h+ Y1 G$ q5 c6 _9 \, jnbars = size(volume_mean,2); |8 k% K, ?2 \* \' g
groupwidth =min(0.8, nbars/(nbars+1.5));
$ A3 z- w2 G0 \( |% X4 z5 L% V( T) d3 U3 |3 N$ G6 Q
/ w; ]8 T2 W! p5 W4 P% errorbar如果用不同颜色,可以利用colormap的颜色进行循环标记,这个例子没有用到colormap
2 }0 h1 f& m* W7 h/ X! b3 N%colormap(flipud([0 100/255 0; 220/255 20/255 60/255; 1 215/255 0; 0 0 1])); % blue / red
' c+ Z* z$ W) o9 `# L W% color=[0 100/255 0; 220/255 20/255 60/255; 1 215/255 0; 0 0 1];
% Q( J& ? h- E) o+ S; f8 Ehold on;
% P1 y# H8 S A$ e+ Ifor i = 1:nbars9 ~3 a5 C) p( X+ `3 }
x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);
; t! f: D. |( G# G4 m( e4 u errorbar(x,volume_mean(:,i),volume_std(:,i),'o','color',[.5 .5 .5],'linewidth',2);
* I" i' R+ {* v6 ^& \5 Uend
+ y2 z7 N5 n& w0 t/ Y* ^( n/ N& ]% i( S2 [
8 [( H6 Y% T' D
set(gca,'XTickLabel',{'2014','2015','2016'},'fontsize',14,'linewidth',2)
" @/ o8 A1 c2 Uylim([0 1.5]) r" d4 R6 F! ?" }7 G
set(gca,'ytick',0:0.5:1.5)
N+ V/ j' u# B8 n+ h( o: dxylabel(gca,' ','Volume[Sv]')
- |+ X9 f w5 Q {- o- Z flegend('data1','data2','location','NorthEast')
- G S& T% ?5 W+ g" ]' Z% |1 d4 }' h% r3 d) i
i; A# F9 l& f2 Q以上实例可以参考使用。
; q9 A/ P/ l' E% j
! d' r$ z/ G: b! G. a+ U" Y3 i
, E' l; b6 _! C& G8 kerrorbar的局部调整:8 }" t5 Z- S+ k$ x1 f: U( _
1.头部宽度调整2 \: @& b g; |+ c1 y- h
% Create errorbar
' g( y$ y g q" b) @X = 0:pi/10:pi;
( s) p" Z9 `& ]' Q( p2 J5 u! ~. X L" kY = sin(X) + 1;
& q7 w0 K6 ^" B9 iE = std(Y) * ones(size(X));
' T, m) a- |# v6 j% g; Y9 \! p" A- iha = errorbar(X, Y, E);' ]; \ w8 W/ x/ L" b2 Q8 |' X
% Width of the top and bottom lines of errorbar
! ~$ w8 ^5 ^; E1 k+ S6 X7 Xxlength = 0.2;' K# S- f4 K% ^) L n
% Make horizontal lines with 'line'8 c- M$ ^( h8 U& k7 z
for k = 1:length(X)- ^4 {' F& j* t
x = [X(k) - xlength, X(k) + xlength];6 P" ]: u, ^" [8 E0 |
y_h = [Y(k) + E(k), Y(k) + E(k)];9 G* K5 h- {3 c8 n( f, _* K
line(x, y_h);
: d! Q: q# d" }- @ y_b = [Y(k) - E(k), Y(k) - E(k)];! x/ ?$ X% _' o. u, E! J* j" I
line(x, y_b);
$ X+ o8 D5 n2 h) _end: l. s, V! ^4 z/ {
参考:www.52ocean.cn
; N' V" W- F) P: B
5 c# M! z. O8 V) l& K U O; h7 Y T% y( B* t
4 A. \; u0 J* |0 {& i
6 c& x) _' V2 s8 [, c 转载本文请联系原作者获取授权,同时请注明本文来自叶瑞杰科学网博客。 |