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

872 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 17 Mar 2021
Edited: MathWorks Support Team on 17 Mar 2021
The ability to specify that the "errorbar" function should display the error bars inside the patches is not available in MATLAB.
There are two work arounds for this limitation, usage of which depends on the release of MATLAB that you are using.
For R2019a or earlier releases:
Find the center of each bar and pass this data into "errorbar" with the respective error values.
% Example data
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];
b = bar(model_series, 'grouped');
hold on
% Find the number of groups and the number of bars in each group
[ngroups, nbars] = size(model_series);
% Calculate 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
hold off
For R2019b or later releases:
Retrieve the x coordinate of each bar using the "XEndPoints" property and pass this data into "errorbar":
% Example data
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];
b = bar(model_series, 'grouped');
hold on
% Calculate the number of groups and number of bars in each group
[ngroups,nbars] = size(model_series);
% Get the x coordinate of the bars
x = nan(nbars, ngroups);
for i = 1:nbars
    x(i,:) = b(i).XEndPoints;
end
% Plot the errorbars
errorbar(x',model_series,model_error,'k','linestyle','none');
hold off
 
  22 Comments
ZenithWoman
ZenithWoman on 27 Jun 2021
Edited: ZenithWoman on 27 Jun 2021
Try this
Y = [0.00083045,0.00092545,0.00090585;0.000864475,0.000883675,...
0.00091715;0.0008724,0.000900175,0.00094375;0.00092295,0.000937475,...
0.000954925;0.0010085,0.00103575,0.00111325;0.0012755,0.001303,0;...
0.001835,0.0018805,0];%Y data
Y_er = [2.65534E-06,3.43093E-05,1.63905E-05;2.87529E-06,2.50012E-06...
1.15358E-05;2.7377E-06,2.45064E-06,1.08844E-05;7.01029E-06,1.879E-06,...
2.48475E-06;2.53311E-06,2.49583E-06,3.06526E-06; 3.32916E-06,...
1.77951E-06,0;4.50925E-06,4.05175E-06,0];%The standard error of the means
cat_label = categorical({'Water';'#20-2.5';'#20-5';'#20-10'; '#20-20';'#20-50';'#20-100'}); %The Group Label
cat_label = reordercats(cat_label,{'Water';'#20-2.5';'#20-5';'#20-10'; '#20-20';'#20-50';'#20-100'});%To Preserve Label Order
b = bar(Y,'grouped');
hold on
% Calculate the number of groups and number of bars in each group
[ngroups,nbars] = size(Y);
% Get the x coordinate of the bars
x = nan(nbars, ngroups);
for i = 1:nbars
x(i,:) = b(i).XEndPoints;
end
errorbar(x',Y,Y_er,'k','linestyle','none');%Adding the errorbars
hold off
set(gca,'xticklabel',cat_label)%This line should replace the numbers with your categorical label
Good luck!

Sign in to comment.

More Answers (1)

Iron1759
Iron1759 on 21 Jan 2021
Edited: Iron1759 on 21 Jan 2021
I'm almost there..
I created a dynamic bar chart that compares different sets of data, it works partially (i.e. at XEndPoints).
How do I use another locations matix to adjust the YEndPoints as well?

Products


Release

No release entered yet.

Community Treasure Hunt

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

Start Hunting!