How to plot errorbars in a grouped bar?

I want to plot errorbar in my grouped bars. I wrote following code

x = categorical({'a', 'b', 'c', 'd', 'e'});
y = [6816,7215; 5824,6180; 4860,5200; 3860,4206; 2838,3185];
errlow = [238,337;270,355;303,297;291,340;259,382];
errhigh = [231,264;225,337;153,171;185,286;167,247];
b = bar(x,y);
hold on
xtips1 = b(1).XEndPoints;
ytips1 = b(1).YEndPoints;
er = errorbar(xtips1, ytips1, errlow(:,1), errhigh(:,1));
er.Color = [0 0 0];
er.LineStyle = 'none';
hold on
xtips2 = b(2).XEndPoints;
ytips2 = b(2).YEndPoints;
er2 = errorbar(xtips2, ytips2, errlow(:,2), errhigh(:,2));
er2.Color = [0 0 0];
er2.LineStyle = 'none';
hold off
ylim([2400 7600]);

Errorbars are displaying into the graph. Please have a look, they are straight over a, b, c, d and e but not on the bar as shown in the figure.

enter image description here

I want them on the associated bars ('GREEN' marked error should be on right bar whereas 'RED' marked error should be on left bar) as presented in the figure. How do I do that?

Thanks in advance!

1

1 Answer

This is the answer provided by the MathWorks Support Team, as posted on MATLAB Answers (with the exception of some minor modifications).


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 the above:

% 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');

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

enter image description here

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like