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
## 12 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

This code works great - been able to test it for different types of bargraphs. However, it will be fantastic to understand

howorwhythe following lines of code work:and

Thank you!

## 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

TK1234 answered with "You are brilliant". I moved his answer to the comment section here.

## 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

This works very well. Thank you very much.

## 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

This generated a great looking bar plot! Thank you for sharing.

## 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

Thank you! The code works great!

## 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

Thank you! This code works well also for me! But, if i want to plot with this code only upper or lower error?

## 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

It worked except that the error bars are to the side and not to the center of each bar. How would I correct this?

## 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

Hi Miraflor, this answer has been recently updated.

If the above method still results in uncentered error bars, I would suggest you look primarily at the 3rd-to-last line where the locations are calculated, or contact MathWorks Technical Support for further assistance.

## 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

it keeps say x to left is invalid assignment

## 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

Just required brackets, should work now if you replace with this line instead of what is there

x = (1:ngroups) - (groupwidth/2) + (2*i-1) * (groupwidth / (2*nbars));

## 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

Could someone explain why the groupwidth is calculated with this?

## 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

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.

Sign in to comment.