在数据统计分析时,往往会用到多个数据分组进行比较,这里介绍一种数据可视化方法。
' v o- j! h$ P+ P实例:
- {; r) ?; t+ C" X/ w* D% 数据
$ Z3 V' O; x& t7 V% h t7 {- Vvolume_mean=[0.73,0.45;, y2 K0 r8 T6 x! o
0.42,0.43;
2 T* A1 R6 l) X w% A 0.70,0.42];
0 S( Y* C' o' z$ W2 ^5 z4 G# Y$ [volume_std=[0.65,0.17;! N3 f+ p7 l- d2 H+ j ?% N
0.35,0.14;, j m; W" P n6 w R3 c
0.44,0.13];3 b f' m& g- ^: Q
%绘图 0 W" X( i7 Z& P, P" R
close all;figure;7 j/ K6 N6 S7 w6 |
h=bar(volume_mean);
8 c0 F* V* X7 W+ Z8 o$ sset(h,'BarWidth',0.9); % 柱状图的粗细# f/ P6 s% ^, C
hold on;
S! ^. z0 T9 n& R& i aset(h(1),'facecolor',[139 35 35]./255) % 第一列数据视图颜色
0 x( |2 H4 _- ~set(h(2),'facecolor','k') % 第二列数据视图颜色) ~, ]3 d1 D- N- [
3 |) X3 E+ t" E4 T! S( Y" x" O7 G! v0 G- H3 m
ngroups = size(volume_mean,1);
. P2 y; X. e" y" z' s. [nbars = size(volume_mean,2);7 w+ O: J, i* t1 E4 M; s" g
groupwidth =min(0.8, nbars/(nbars+1.5));
/ f( l4 I7 h- Q/ a, d5 z* r V* J) C
) t! A# d8 k! f' f" G1 X8 A5 H( I
% errorbar如果用不同颜色,可以利用colormap的颜色进行循环标记,这个例子没有用到colormap
6 x/ y, f5 \+ l) \" S' _' U5 @0 d%colormap(flipud([0 100/255 0; 220/255 20/255 60/255; 1 215/255 0; 0 0 1])); % blue / red" x7 h" T, Z+ @+ u( Q8 @
% color=[0 100/255 0; 220/255 20/255 60/255; 1 215/255 0; 0 0 1];
) f" f0 j! m7 G/ h: zhold on;7 g* T c/ Q5 ?6 \6 s8 B8 K
for i = 1:nbars
: h# i/ j! ] x3 S x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);, {$ _- G$ \/ q; z- q
errorbar(x,volume_mean(:,i),volume_std(:,i),'o','color',[.5 .5 .5],'linewidth',2);
+ `. M# @) [; G, V. ?/ g2 I: iend
! D9 e+ `' v3 u( u1 S& i& r5 v7 X6 j$ c
, g7 `% ~* g J- C; M$ {# k
+ W, S$ }- D0 ~& n0 {* hset(gca,'XTickLabel',{'2014','2015','2016'},'fontsize',14,'linewidth',2)
3 X5 x: l9 v( v- K5 n8 b9 B3 M. Q2 S; Bylim([0 1.5])( G6 F) z4 n0 {8 n1 |+ i
set(gca,'ytick',0:0.5:1.5)
! B7 U* G% o9 ^. ]" G# Axylabel(gca,' ','Volume[Sv]') Z l1 M! P" [4 a# q! F9 Y
legend('data1','data2','location','NorthEast')2 Q. y L/ p* B
/ [$ [+ L6 v$ E& T. Z$ \9 N
# i7 e2 t" Y1 q. @- m以上实例可以参考使用。0 W$ ], D" a6 @
2 e0 I9 y, I" F- [
0 A' ~7 A5 A0 ~$ E
errorbar的局部调整:
- Z d* t6 J7 O1.头部宽度调整7 d! Q$ `+ b3 Q0 j& \
% Create errorbar
" F, X& J; l3 u. `& m6 v& V% ^X = 0:pi/10:pi;' S4 E# l8 N1 F6 ?
Y = sin(X) + 1;1 Z- V# n8 r7 N O" _
E = std(Y) * ones(size(X));) h, }( A$ s) ?8 [1 `; R% R
ha = errorbar(X, Y, E);
4 R3 T) y. O6 ]6 u% Width of the top and bottom lines of errorbar
' z7 ~# \, D7 P7 [/ @3 M" ^xlength = 0.2;* c4 ~ R5 C$ j
% Make horizontal lines with 'line'% h5 G* w6 X+ p9 _8 P- m8 F1 M* K. C
for k = 1:length(X)% _4 [( f: v; y5 P' Y
x = [X(k) - xlength, X(k) + xlength];
I4 h# o% \1 f/ K y_h = [Y(k) + E(k), Y(k) + E(k)];4 u w: W) V$ Z! \1 t
line(x, y_h);
) w5 f- ^ Y2 K. C, { y_b = [Y(k) - E(k), Y(k) - E(k)];% d5 _7 |- ]# y. I
line(x, y_b);2 i B. I, u- ~, e" a
end
7 S, v& |' m: M) g9 X; D! s) J参考:www.52ocean.cn . L- `8 G+ x( [4 u4 N
* H1 ^! Z4 u4 a% p" w F5 ^2 F F/ `9 Z) a
( h+ m, _! N9 j( T& k
u' W3 O9 u0 p' Z4 V* M' e 转载本文请联系原作者获取授权,同时请注明本文来自叶瑞杰科学网博客。 |