-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_areaerrorbar.m
80 lines (72 loc) · 3.76 KB
/
plot_areaerrorbar.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
% ----------------------------------------------------------------------- %
% Function plot_areaerrorbar plots the mean and standard deviation of a %
% set of data filling the space between the positive and negative mean %
% error using a semi-transparent background, completely customizable. %
% %
% Input parameters: %
% - data: Data matrix, with rows corresponding to observations %
% and columns to samples. %
% - options: (Optional) Struct that contains the customized params.%
% * options.handle: Figure handle to plot the result. %
% * options.color_area: RGB color of the filled area. %
% * options.color_line: RGB color of the mean line. %
% * options.alpha: Alpha value for transparency. %
% * options.line_width: Mean line width. %
% * options.x_axis: X time vector. %
% * options.error: Type of error to plot (+/-). %
% if 'std', one standard deviation; %
% if 'sem', standard error mean; %
% if 'var', one variance; %
% if 'c95', 95% confidence interval. %
% ----------------------------------------------------------------------- %
% Example of use: %
% data = repmat(sin(1:0.01:2*pi),100,1); %
% data = data + randn(size(data)); %
% plot_areaerrorbar(data); %
% ----------------------------------------------------------------------- %
% Author: Victor Martinez-Cagigal %
% Date: 30/04/2018 %
% E-mail: vicmarcag (at) gmail (dot) com %
% ----------------------------------------------------------------------- %
% Adapted by M.N. April. 2019
% corrected sem and c95 calculation
% added compatibility for NaNs and if std==0;
function plot_areaerrorbar(data, options)
% Default options
if(nargin<2)
options.handle = figure(1);
options.color_area = [128 193 219]./255; % Blue theme
options.color_line = [ 52 148 186]./255;
%options.color_area = [243 169 114]./255; % Orange theme
%options.color_line = [236 112 22]./255;
options.alpha = 0.5;
options.line_width = 2;
options.error = 'sem';
end
% Computing the mean and standard deviation of the data matrix
data_mean = nanmean(data,1);
data_std = nanstd(data,0,1);
% NaN compatibility
data_std(isnan(data_std)) = 0;
data(:,isnan(data_mean)) = [];
data_std(isnan(data_mean)) = [];
data_mean(isnan(data_mean)) = [];
if(isfield(options,'x_axis')==0), options.x_axis = 1:size(data,2); end
options.x_axis = options.x_axis(:);
% Type of error plot
switch(options.error)
case 'std', error = data_std;
case 'sem', error = (data_std./sqrt(sum(~isnan(data)))); % M.N.
case 'var', error = (data_std.^2);
case 'c95', error = (data_std./sqrt(sum(~isnan(data)))).*1.96; % M.N:
end
% Plotting the result
figure(options.handle);hold all;
x_vector = [options.x_axis', fliplr(options.x_axis')];
patch = fill(x_vector, [data_mean+error,fliplr(data_mean-error)], options.color_area);
set(patch, 'edgecolor', 'none');
set(patch, 'FaceAlpha', options.alpha);
plot(options.x_axis, data_mean, 'color', options.color_line, ...
'LineWidth', options.line_width);
hold off;
end