Asked by MathWorks Support Team
on 16 Dec 2011

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.

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

Opportunities for recent engineering grads.

Apply Today
## 13 Comments

## KV (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/102220-how-do-i-place-errorbars-on-my-grouped-bar-graph-using-function-errorbar-in-matlab#comment_396159

## Oleg Komarov (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/102220-how-do-i-place-errorbars-on-my-grouped-bar-graph-using-function-errorbar-in-matlab#comment_424879

## Brian Muldoon (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/102220-how-do-i-place-errorbars-on-my-grouped-bar-graph-using-function-errorbar-in-matlab#comment_426170

## Vera Elizabeth Thornton (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/102220-how-do-i-place-errorbars-on-my-grouped-bar-graph-using-function-errorbar-in-matlab#comment_449208

## LI (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/102220-how-do-i-place-errorbars-on-my-grouped-bar-graph-using-function-errorbar-in-matlab#comment_450413

## Alessandro Leparulo (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/102220-how-do-i-place-errorbars-on-my-grouped-bar-graph-using-function-errorbar-in-matlab#comment_472099

## Miraflor Santos (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/102220-how-do-i-place-errorbars-on-my-grouped-bar-graph-using-function-errorbar-in-matlab#comment_472194

## Michael Abboud (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/102220-how-do-i-place-errorbars-on-my-grouped-bar-graph-using-function-errorbar-in-matlab#comment_473773

## Jonathan Duncan (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/102220-how-do-i-place-errorbars-on-my-grouped-bar-graph-using-function-errorbar-in-matlab#comment_491105

## Mike (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/102220-how-do-i-place-errorbars-on-my-grouped-bar-graph-using-function-errorbar-in-matlab#comment_509108

## sdm sdm (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/102220-how-do-i-place-errorbars-on-my-grouped-bar-graph-using-function-errorbar-in-matlab#comment_564387

## Erica Kiderman (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/102220-how-do-i-place-errorbars-on-my-grouped-bar-graph-using-function-errorbar-in-matlab#comment_568314

## Abby Lo (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/102220-how-do-i-place-errorbars-on-my-grouped-bar-graph-using-function-errorbar-in-matlab#comment_583624

Sign in to comment.