How to produce a grouped errorbars?

1 view (last 30 days)
Tomaszzz
Tomaszzz on 28 Mar 2022
Commented: Tomaszzz on 29 Mar 2022
Hi all,
I am trying to produce a grouped errorbars with the attached data.
"Mean' is 5x3 matrix with means and 'std' is the same with standard deviations of those means.
The ouput figure should look like more or less like this, with the exception I should have 5 error bars per group (not 3 like in the image), with diffrent colours for means.
More precisle what I mean, using an example of just one group
figure
subplot(3,3,1)
Group 'Sag'
errorbar(mean(1,1), std(1,1),'-s','MarkerSize',10,...
'MarkerEdgeColor','red','MarkerFaceColor','red', 'LineWidth', 1);
errorbar(mean(2,1), std(2,1),'-s','MarkerSize',10,...
'MarkerEdgeColor','green','MarkerFaceColor','green', 'LineWidth', 1);
errorbar(mean(3,1), std(3,1),'-s','MarkerSize',10,...
'MarkerEdgeColor','black','MarkerFaceColor','black', 'LineWidth', 1);
errorbar(mean(4,1), std(4,1),'-s','MarkerSize',10,...
'MarkerEdgeColor','yellow','MarkerFaceColor','yellow', 'LineWidth', 1);
errorbar(mean(5,1), std(5,1),'-s','MarkerSize',10,...
'MarkerEdgeColor','blue','MarkerFaceColor','blue', 'LineWidth', 1);
Group 'Frnt'
errorbar(mean(1,2), std(1,2),'-s','MarkerSize',10,...
'MarkerEdgeColor','red','MarkerFaceColor','red', 'LineWidth', 1);
etc
Can you help please?

Accepted Answer

Voss
Voss on 28 Mar 2022
Edited: Voss on 28 Mar 2022
Maybe something like this:
load('mean.mat')
load('std.mat')
mean = mean.';
figure();
ax = gca();
hold on
% to be adjusted for different numbers of groups or bars/group:
group_names = {'Sag','Frnt','Tran'};
bar_colors = 'rgkyb';
[n_groups,n_bars] = size(std);
x_min = 0.5;
x_max = n_groups*n_bars+0.5;
y_min = min(mean(:)-std(:));
y_max = max(mean(:)+std(:));
dy = y_max-y_min;
y_min = y_min-0.05*dy;
y_max = y_max+0.05*dy;
% grey shading on top:
p = patch( ...
'Parent',ax, ...
'XData',[x_min x_max x_max x_min], ...
'YData',[0.75 0.75 1 1]*(y_max-y_min)+y_min, ... % not sure exactly where the grey box goes this time
'FaceColor',[0.8 0.8 0.8], ...
'EdgeColor','none');
% line separating axes into 3 parts:
gl = line( ...
'Parent',ax, ...
'XData',[1 1 NaN 2 2]/3*(x_max-x_min)+x_min, ...
'YData',[y_min y_max NaN y_min y_max]);
% error bars:
for ii = 1:n_bars
h = errorbar((0:n_groups-1)*n_bars+ii,mean(:,ii),std(:,ii),'-sk', ...
'MarkerSize',10,...
'MarkerEdgeColor',bar_colors(ii), ...
'MarkerFaceColor',bar_colors(ii), ...
'LineWidth',1, ...
'LineStyle','none');
end
set(ax, ...
'Box','on', ...
'Layer','top', ...
'XLim',[x_min x_max], ...
'YLim',[y_min y_max], ...
'XTick',(n_bars+1)/2+n_bars*(0:n_groups-1), ...
'XTickLabel',group_names);
  3 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!