Continuous error band in Matlab

I was looking for a plot function in Matlab that could create error “bands” with a continuous filled plot for the standard error, highlighting the mean. In the end I decided to create the function I needed so that I could have the parameters I wanted: namely colours and transparency.

filled_plot_example

Here an example of how to use two versions of the same function, one for matrices and the second for a structure of data. Download the archive with the two functions.

clear all
close all
figure(‘Position’, [200 200 1500 500]);

%% test with consistent sample of data
data_mat1 = rand(10,20); % first set of data: mean and standard error are calculated per column
data_mat2 = rand(10,20); % second set of data: mean and standard error are calculated per column
color1 = [0.1 0.7 0.3]; % color used for the first set of data
color2 = [0.2 0.2 0.7]; % color used for the second set of data
t1 = 1.0; % transparency of the first fill plot (1 = solid, 0 = transparent)
t2 = 0.5; % transparency of the second fill plot (1 = solid, 0 = transparent)

subplot(1,2,1)
filled_plot(data_mat1, color1, t1, data_mat2, color2, t2)

%% test with uneven sample of data
for i = 1:20
data_mat3{i} = rand(10 + i*10, 1); % first set of data: mean and standard error are calculated per cell
data_mat4{i} = rand(10 + i*10, 1); % second set of data: mean and standard error are calculated per cell
end

color3 = [0.5 0.5 0.5]; % color used for the first set of data
color4 = [0.5 0.1 0.1]; % color used for the second set of data
t3 = 0.8; % transparency of the first fill plot (1 = solid, 0 = transparent)
t4 = 0.4; % transparency of the second fill plot (1 = solid, 0 = transparent)

subplot(1,2,2)
filled_plot_cell(data_mat3, color3, t3, data_mat4, color4, t4)

 

Function 1 (matrix of values)

function fig = filled_plot(input_vecs1, color_mean1, transparency1, input_vecs2, color_mean2, transparency2)

mean_vec1 = mean(input_vecs1);
mean_vec2 = mean(input_vecs2);

error_vec1 = std(input_vecs1) / sqrt(length(input_vecs1(:,1)));
error_vec2 = std(input_vecs2) / sqrt(length(input_vecs2(:,1)));

color_error1 = min(1, color_mean1 + 0.3);
color_error2 = min(1, color_mean2 + 0.3);

x1 = 1:length(mean_vec1);
x2 = 1:length(mean_vec2);

X1 = [x1, fliplr(x1)];
X2 = [x2, fliplr(x2)];

y1 = mean_vec1 + error_vec1 / 2;
y3 = mean_vec2 + error_vec2 / 2;

y2 = mean_vec1 – error_vec1 / 2;
y4 = mean_vec2 – error_vec2 / 2;

Y1 = [y1, fliplr(y2)];
Y2 = [y3, fliplr(y4)];

hold on
fig{1} = fill(X1, Y1, color_error1, ‘EdgeColor’, color_error1);
fig{2} = plot(mean_vec1, ‘Color’, color_mean1);
fig{3} = fill(X2, Y2, color_error2, ‘EdgeColor’, color_error2);
fig{4} = plot(mean_vec2, ‘Color’, color_mean2);

set(fig{1}, ‘facealpha’, transparency1);
set(fig{3}, ‘facealpha’, transparency2);

hold off

end

Function 2 (structure of values)

function fig = filled_plot_cell(input_struct1, color_mean1, transparency1, input_struct2, color_mean2, transparency2)

for i = 1:length(input_struct1)
mean_vec1(i) = mean(input_struct1{i});
mean_vec2(i) = mean(input_struct2{i});
error_vec1(i) = std(input_struct1{i}) / sqrt(length(input_struct1{i}));
error_vec2(i) = std(input_struct2{i}) / sqrt(length(input_struct2{i}));
end

color_error1 = min(1, color_mean1 + 0.3);
color_error2 = min(1, color_mean2 + 0.3);

x1 = 1:length(mean_vec1);
x2 = 1:length(mean_vec2);

X1 = [x1, fliplr(x1)];
X2 = [x2, fliplr(x2)];

y1 = mean_vec1 + error_vec1 / 2;
y2 = mean_vec1 – error_vec1 / 2;

y3 = mean_vec2 + error_vec2 / 2;
y4 = mean_vec2 – error_vec2 / 2;

Y1 = [y1, fliplr(y2)];
Y2 = [y3, fliplr(y4)];

hold on
fig{1} = fill(X1, Y1, color_error1, ‘EdgeColor’, color_error1);
fig{2} = plot(mean_vec1, ‘Color’, color_mean1);
fig{3} = fill(X2, Y2, color_error2, ‘EdgeColor’, color_error2);
fig{4} = plot(mean_vec2, ‘Color’, color_mean2);

set(fig{1}, ‘facealpha’, transparency1);
set(fig{3}, ‘facealpha’, transparency2);

hold off

end

 

 

Insert math as
Block
Inline
Additional settings
Formula color
Text color
#333333
Type math using LaTeX
Preview
\({}\)
Nothing to preview
Insert