在数据统计分析时,往往会用到多个数据分组进行比较,这里介绍一种数据可视化方法。/ R5 v+ m) x7 D3 X- U4 V
实例:+ P* Q1 o6 H/ d0 c
% 数据
7 t# v& \! }$ w" |+ _volume_mean=[0.73,0.45;
" R, X2 N6 T& x1 J. e+ E 0.42,0.43;( k. e+ U/ Y0 H6 O E
0.70,0.42];
( c5 h+ r+ }% f! [+ hvolume_std=[0.65,0.17;
1 y2 m9 J$ G @( o, Y4 F1 D 0.35,0.14;* c, S" {, E- ]. W. d% L0 K
0.44,0.13];1 \2 {5 } |7 R: h- l
%绘图 & T% o$ c2 Y, X( G! [: Z Y4 ]
close all;figure;3 H3 z- C) u" Y9 b5 k6 p
h=bar(volume_mean);
% i m5 x5 w! h# ^. Wset(h,'BarWidth',0.9); % 柱状图的粗细2 Q. l& l; R+ m/ P& K# X' d
hold on;) R+ y/ ? V+ y% N
set(h(1),'facecolor',[139 35 35]./255) % 第一列数据视图颜色3 F. b+ ^" v5 r' {
set(h(2),'facecolor','k') % 第二列数据视图颜色
3 L3 h9 y. o8 S. j! U
3 J S z9 _% j& x* ~4 i8 K/ C. n6 @7 R
ngroups = size(volume_mean,1);
. J& \0 @3 N# A9 o" _) \nbars = size(volume_mean,2);/ s5 U- [5 }" R- q2 C/ j0 ^7 ^( K
groupwidth =min(0.8, nbars/(nbars+1.5));0 S$ X8 G! _" V' b2 a7 n! f! r
) h* _) m7 {# e6 A2 I/ t) g; K9 A; ^7 y4 k
% errorbar如果用不同颜色,可以利用colormap的颜色进行循环标记,这个例子没有用到colormap
- H4 c8 @1 J6 e! O' V%colormap(flipud([0 100/255 0; 220/255 20/255 60/255; 1 215/255 0; 0 0 1])); % blue / red
( S" v6 x! c/ i% color=[0 100/255 0; 220/255 20/255 60/255; 1 215/255 0; 0 0 1];
+ H1 \ Q* |' Chold on;! c. b3 n1 q! _, S5 i# g
for i = 1:nbars
]7 q) x% x2 r+ l3 d x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);4 t4 D+ n$ b, s; O! {
errorbar(x,volume_mean(:,i),volume_std(:,i),'o','color',[.5 .5 .5],'linewidth',2);
6 L' x; Y: Y% i6 bend1 r# d0 x4 t$ ~( @5 g& L2 p* ^
: S; A' x" x& T! @0 ]- F2 n
% \/ X u. J$ i* r2 E, e; Oset(gca,'XTickLabel',{'2014','2015','2016'},'fontsize',14,'linewidth',2): H& u4 F2 Y- [2 |. Z& o+ V$ z- r
ylim([0 1.5]) o! Q# j' G ^; c5 e+ V3 P
set(gca,'ytick',0:0.5:1.5)
, R4 I* e, r e T5 S; Qxylabel(gca,' ','Volume[Sv]')
' Y3 F7 s, Y& c- l# L2 @8 ^legend('data1','data2','location','NorthEast')# F3 ?! j6 ~! o1 c o$ L+ r" C
( E: k) ^( L* U1 s }: n+ Y: T& I2 A$ }. q7 B, L3 o* N
以上实例可以参考使用。
2 S% a- h" L! U8 U& K
+ n' H7 j! d4 G' f3 S) I3 ?4 L( q
) ~9 L0 y/ U0 M0 berrorbar的局部调整:
! L& n* _% U) E1.头部宽度调整5 {* X$ P+ {0 d$ D: S- W z5 r
% Create errorbar1 r+ U" j. L0 d/ [6 u
X = 0:pi/10:pi;
7 g& |+ @* X4 _, LY = sin(X) + 1;
% g' y5 o; x% M% g8 cE = std(Y) * ones(size(X));
& I: {0 m9 j) s0 I* eha = errorbar(X, Y, E);$ h# R/ Z+ U6 D k( g: [
% Width of the top and bottom lines of errorbar2 p O7 t. s. b0 [: t& r+ F9 {2 ]$ i
xlength = 0.2;
( Q; `, n. I" G1 t% Make horizontal lines with 'line'6 h* n- C; U; B4 x" y
for k = 1:length(X)$ O3 }9 ~9 ~: U7 W& U, @% b4 o
x = [X(k) - xlength, X(k) + xlength];
. m7 P! f. i- X y_h = [Y(k) + E(k), Y(k) + E(k)];: Y) i5 _% e) v! r
line(x, y_h);
; g+ E9 ~$ U: R$ y8 g y_b = [Y(k) - E(k), Y(k) - E(k)];
5 H2 M1 F" }/ u% \ line(x, y_b);$ U, I6 s, o' D1 ?/ Y C" f% {
end
$ _( \3 z k" D7 {" G参考:www.52ocean.cn " ^3 o, A9 W. C' s' A/ n# ^
: V9 C" f" l% p L2 B5 C
1 G1 A8 z0 ^) d% j" A
( a! ~% q, T0 m7 E* }- k
# f F$ c# B4 y. E/ ] 转载本文请联系原作者获取授权,同时请注明本文来自叶瑞杰科学网博客。 |