MATLAB Answers

3

How do I place errorbars on my grouped bar graph using function ERRORBAR in MATLAB ?

I use the following to create a grouped bar graph:
model_series = [10 40 50 60; 20 50 60 70; 30 60 80 90];
model_error = [1 4 8 6; 2 5 9 12; 3 6 10 13];
bar(model_series, 'grouped');
hold on
errorbar( model_series,model_error)
The result is a grouped bar graph with error bars placed in between each group of bars.
I would like to have 'ERRORBAR' place error bars on the center of each bar.

Products


Release

No release entered yet.

1 Answer

Answer by MathWorks Support Team on 15 May 2018
Edited by MathWorks Support Team on 15 May 2018
 Accepted Answer

The ability to specify that the ERRORBAR function display the error bars inside the patches is not available in MATLAB.
To work around this issue, find the center of each bar and pass this data into ERRORBAR with the respective error values. The following is an example of this:
% Data to be plotted as a bar graph
model_series = [10 40 50 60; 20 50 60 70; 30 60 80 90];
%Data to be plotted as the error bars
model_error = [1 4 8 6; 2 5 9 12; 3 6 10 13];
% Creating axes and the bar graph
ax = axes;
h = bar(model_series,'BarWidth',1);
% Set color for each bar face
% h(1).FaceColor = 'blue';
% h(2).FaceColor = 'yellow';
% Properties of the bar graph as required
ax.YGrid = 'on';
ax.GridLineStyle = '-';
xticks(ax,[1 2 3]);
% Naming each of the bar groups
xticklabels(ax,{ 'Low', 'Middle', 'High'});
% X and Y labels
xlabel ('Socio Economic Status');
ylabel ('Mean Writing Score');
% Creating a legend and placing it outside the bar plot
lg = legend('A','B','C','D','AutoUpdate','off');
lg.Location = 'BestOutside';
lg.Orientation = 'Horizontal';
hold on;
% Finding the number of groups and the number of bars in each group
ngroups = size(model_series, 1);
nbars = size(model_series, 2);
% Calculating the width for each bar group
groupwidth = min(0.8, nbars/(nbars + 1.5));
% Set the position of each error bar in the centre of the main bar
% Based on barweb.m by Bolu Ajiboye from MATLAB File Exchange
for i = 1:nbars
% Calculate center of each bar
x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);
errorbar(x, model_series(:,i), model_error(:,i), 'k', 'linestyle', 'none');
end

  13 Comments

Could someone explain why the groupwidth is calculated with this?
groupwidth = min(0.8, numbars/(numbars+1.5));
The groupwidth equation comes from the fact that the bar width property is specified as a fraction of the total space available for each bar and is 0.8 by default. In our case, the width of each individual bar (widthperbar) is 1 and the space between bar groups is 1.5 so to get the proper fraction -> numbars*widthperbar/(numbars*widthperbar+1.5) in this case since the width per bar is 1, that comes out to numbars/(numbars+1.5).
It also appears the code had a hidden invalid text character, which should explain the error. Thank you for bringing this to our attention. I have edited the answer above and there should no longer be an invalid character and should function as posted.
I tried to run it in MATLAB R2017b but the error bars didn't show up. It showed the following error
Error (line 35) errorbar(x, model_series(:,i), model_error(:,i), 'k', 'linestyle', 'none');

Sign in to comment.