Mercurial > hg > ltpda
comparison m-toolbox/classes/+utils/@math/boxplot.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 % BOXPLOT draw box plot on data | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: Boxplot is a convenient way of graphically depicting groups | |
5 % of numerical data. The bottom and top of the box are always the 25th and | |
6 % 75th percentile (the lower and upper quartiles, respectively), and the | |
7 % band near the middle of the box is always the 50th percentile (the | |
8 % median). The ends of the whiskers are the percentiles corresponding to | |
9 % the confidence level defined by the user. | |
10 % | |
11 % CALL: boxplot(d1,...,dn, ops) | |
12 % | |
13 % INPUT: - d1,...dn, are column vectors. Different data must be input | |
14 % as different columns. | |
15 % - ops, a struct containing input parameters | |
16 % - PlotData. Decide to plot all data or only the data | |
17 % outside the confidence levels. Default true | |
18 % - ConfLevel. Confidence level for box whiskers. Default 95% | |
19 % - FontSize. Font size for x and y labels. Default 22 | |
20 % - LineWidth. Box line width. Default 2 | |
21 % - BoxWidth. the width of the box. Default 1 | |
22 % - Marker. Data marker type. Default '.' | |
23 % - MarkerSize. Data marker size. Default 1 | |
24 % - XTickLabel. A cell containing a cell aray of x tick | |
25 % labels. Default {{''}} | |
26 % | |
27 % acknowledgements | |
28 % A special thanks to Shane Lin who submitted the boxPlot function in | |
29 % Matlab Central to which the present function is inspired | |
30 % | |
31 % | |
32 % Luigi Ferraioli 13-02-2011 | |
33 % | |
34 % $Id: boxplot.m,v 1.4 2011/02/22 14:02:03 luigi Exp $ | |
35 % | |
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
37 function boxplot(varargin) | |
38 | |
39 %%% check and set imput options | |
40 % Default input struct | |
41 defaultparams = struct(... | |
42 'PlotData',true,... | |
43 'ConfLevel',0.95,... | |
44 'FontSize',22,... | |
45 'LineWidth',2,... | |
46 'BoxWidth',1,... | |
47 'Marker','.',... | |
48 'MarkerSize',1,... | |
49 'XTickLabel',{{''}}); | |
50 | |
51 | |
52 names = {'PlotData','ConfLevel','FontSize','LineWidth','BoxWidth',... | |
53 'Marker','MarkerSize','XTickLabel'}; | |
54 | |
55 | |
56 N = numel(varargin); | |
57 if isstruct(varargin{N}) | |
58 ops = varargin{N}; | |
59 Nloop = N-1; | |
60 else | |
61 ops = {}; | |
62 Nloop = N; | |
63 end | |
64 | |
65 % collecting input and default params | |
66 if ~isempty(ops) | |
67 for jj=1:length(names) | |
68 if isfield(ops, names(jj)) | |
69 defaultparams.(names{1,jj}) = ops.(names{1,jj}); | |
70 end | |
71 end | |
72 end | |
73 | |
74 conf = defaultparams.ConfLevel; % confidence level for confidence bounds calculation | |
75 if conf>1 | |
76 conf = conf/100; | |
77 end | |
78 plotdata = defaultparams.PlotData; | |
79 fontsize = defaultparams.FontSize; | |
80 lwidth = defaultparams.LineWidth; | |
81 bwidth = defaultparams.BoxWidth; | |
82 mark = defaultparams.Marker; | |
83 mksize = defaultparams.MarkerSize; | |
84 xticklabel = defaultparams.XTickLabel; | |
85 | |
86 | |
87 %%% draw the plot | |
88 drawBox(varargin(1:Nloop),Nloop,conf,plotdata,lwidth,bwidth,mark,mksize) | |
89 | |
90 % set ylabel | |
91 ylabel('Values','FontSize',fontsize) | |
92 % set xlabel | |
93 xlabel('Experiment','FontSize',fontsize) | |
94 | |
95 % set XTickLabel | |
96 if ~isempty(xticklabel{1}) | |
97 set(gca,'XTickLabel',xticklabel) | |
98 end | |
99 | |
100 end | |
101 | |
102 function drawBox(data,Nloop,conf,plotdata,lwidth,bwidth,mark,mksize) | |
103 | |
104 % define box width | |
105 unit = (1-1/(1+Nloop))/(1+9/(bwidth+3)); | |
106 | |
107 figure; | |
108 hold on; | |
109 v = zeros(5,1); | |
110 for ii = 1:Nloop | |
111 % sort data acsending | |
112 sdata = sort(data{ii}); | |
113 % get data median | |
114 v(1) = median(sdata); | |
115 | |
116 % get cdf | |
117 [F,x] = utils.math.ecdf(sdata); | |
118 % set box lower and upper limit, corresponding to a 25% and 75% | |
119 % probability | |
120 v(2) = interp1(F,x,0.25); % 25% limit | |
121 v(3) = interp1(F,x,0.75); % 75% limit | |
122 % set wisker limits | |
123 alpm = (1-conf)/2; | |
124 v(4) = interp1(F,x,alpm); % lower limit | |
125 v(5) = interp1(F,x,1-alpm); % upper limit | |
126 | |
127 % draw data | |
128 if plotdata % plot all data | |
129 stddata = std(sdata); | |
130 % set x for data | |
131 uunit = unit.*exp(-1.*((sdata-v(1)).^2)./(stddata).^2).*9./10; | |
132 xdat = ii-uunit + 2*uunit.*rand(size(sdata)); | |
133 plot(xdat,sdata,[mark 'b'], 'MarkerSize', mksize); | |
134 else % plot only data outside confidence levels | |
135 idx1 = find(sdata<v(4)); | |
136 idx2 = find(sdata>v(5)); | |
137 idx = [idx1;idx2]; | |
138 xdat = ii.*ones(size(idx)); | |
139 plot(xdat,sdata(idx),[mark 'b'], 'MarkerSize', mksize); | |
140 end | |
141 % draw the min line | |
142 plot([ii-unit, ii+unit], [v(4), v(4)], 'k', 'LineWidth', lwidth); | |
143 % draw the max line | |
144 plot([ii-unit, ii+unit], [v(5), v(5)], 'k', 'LineWidth', lwidth); | |
145 % draw middle line | |
146 plot([ii-unit, ii+unit], [v(1), v(1)], 'r', 'LineWidth', lwidth); | |
147 % draw vertical line | |
148 plot([ii, ii], [v(4), v(2)], '--k', 'LineWidth', lwidth); | |
149 plot([ii, ii], [v(3), v(5)], '--k', 'LineWidth', lwidth); | |
150 % draw box | |
151 plot([ii-unit, ii+unit, ii+unit, ii-unit, ii-unit], [v(3), v(3), v(2), v(2), v(3)], 'k', 'LineWidth', lwidth); | |
152 | |
153 | |
154 | |
155 | |
156 end | |
157 | |
158 end |