MATLAB Answers

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

935 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 27 May 2020
Edited: MathWorks Support Team on 27 May 2020
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.
1. If you are using R2019a or earlier releases, find the center of each bar and pass this data into "errorbar" with the respective error values.
2. If you are using R2019b or later releases, retrieve the x coordinate of each bar using the XEndPoints property and pass this data into "errorbar":
The following is an example of this:
% Example data as before
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');
%%For MATLAB R2019a or earlier releases
hold on
% Find the number of groups and the number of bars in each group
ngroups = size(model_series, 1);
nbars = size(model_series, 2);
% 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 MATLAB 2019b or later releases
hold on
% Calculate the number of bars in each group
nbars = size(model_series, 2);
% Get the x coordinate of the bars
x = [];
for i = 1:nbars
x = [x ; b(i).XEndPoints];
end
% Plot the errorbars
errorbar(x',model_series,model_error,'k','linestyle','none')'
hold off

  15 Comments

Abby Lo
Abby Lo on 28 Jun 2018
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');
Jonathan Eicher
Jonathan Eicher on 22 Jun 2020
Whenever I try to implement the second part of the script all the error bars revert to the center of the groups. If I plot the error bars by themselves tehy are all placed correctly spatially, but as soon as I try to apply it to the graph with my bars it all clusters towards the center of each group.
Any advice?
Julia Hoskins
Julia Hoskins on 28 Oct 2020 at 21:20
I'm currently having the same issue as Jonathan Eicher with the 2019b and after script, so if anyone does have any advice for that it would be extremely beneficial.

Sign in to comment.

More Answers (0)

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!