File Exchange

## Bar with errorbars

version 1.3 (1.77 KB) by

This code plots bars with errorbars, as simple as that.

17 Downloads

Updated

As of now (2009-Jul-13), MATLAB doesn't have a built-in function to plot bars with with error bars on top of them. This function solves that problem.

To a beginning user, this provides a very simple way of plotting bar with errorbars with a simple command:
errorbarbar(y,e); % where y is the height of the bars and e is the limit to plot as error bars (of length 2*e, about y).

For an intermediate user, this function provides the handles to the BAR and ERRORBAR lines for further modification.
[b,e] = errorbarbar(y,e); % or ...
[b,e] = errorbarbar(x,y,e); % where x is center location of the group of bars

For an advanced user, this function also provides ways to modify any and all of the bar and line series properties while plotting it in the first place.

Example:
barSettings = {'facealpha',0.5,'edgecolor',[0.5 0.5 0.5]};
lineSettings = {'linestyle','--','linewidth',2};
[b,e] = errorbarbar(x,y,e,barSettings,lineSettings);
set(b,'facecolor',[0 0.5 0]); % set all bars to color green

Hint: You can use the lineSettings variable to use distinct upper and lower limits.

What it DOESN'T do:
1. Plot horizontal bar graphs
2. Work with bar(y,e,barSettings,lineSettings). Instead, use:
bar(x,y,e,barSettings,lineSettings).

DangDang Lang

### DangDang Lang (view profile)

Ioannis Agriomallos

### Ioannis Agriomallos (view profile)

Update to the previous code, since it worked only for stacked bars correctly. Now it should work for grouped as well.

%% get the xdata to plot the error plots
c = b;
ydata = zeros(size(E));
a = c.BarLayout;
if strcmp(a,'stacked')
for i = 1:length(c)
xdata(:,i) = get(c(i),'XData');
for j = 1:i
ydata(:,i) = ydata(:,i) + get(c(j),'YData')';
end
end
else
for i = 1:length(c)
xdata(:,i) = get(c(i),'XData') + get(c(i),'XOffset');
ydata(:,i) = get(c(i),'YData')';
end
end

Ioannis Agriomallos

### Ioannis Agriomallos (view profile)

In answer of Mohamed Salim and justin murphy:

Matlab R2014b and later versions stopped using the 'Children' attribute as stated here: https://www.mathworks.com/help/matlab/graphics_transition/why-is-the-children-property-empty-for-some-objects.html

I changed the code and worked for me on R2016b, so i share just in case it's of help for anyone. You should simply replace the corresponding cell only:

%% get the xdata to plot the error plots
c = b;
ydata = zeros(size(E));
for i = 1:length(c)
xdata(:,i) = get(c(i),'XData');
for j = 1:i
ydata(:,i) = ydata(:,i) + get(c(j),'YData')';
end
end

Mohamed Salim

### Mohamed Salim (view profile)

I have the same issue as Justin

justin murphy

### justin murphy (view profile)

I seem to get the following error no matter which input I put in:

Index exceeds matrix dimensions.

Error in errorbarbar (line 56)
ydata = mean(tempYData(2:3,:))';

Why might this be?? Any suggestions?
Thanks

Alexander Kurz

### Alexander Kurz (view profile)

Is it possible to plot a grouped stacked bar plot with error bars with this code?

Bianca Elena Ivanof

### Bianca Elena Ivanof (view profile)

Hey all,

I'm using r2012a and just downloaded the .zip folder attached thinking the it'll contain a file which, once activated, would incorporate the errorbarbar function into MATLAB for me to use it as any other function. I know how to plot a bar graph using errorbarbar, done that before - but the way I've been shown how to do it doesn't require such a lengthy code. Maybe I'm missing something, but is there a way I can activate the errorbarbar option in MATLAB (without designing my bar graph using the code above)?

David J. Mack

### David J. Mack (view profile)

Replace the third cell by the following code and it will work:

%% get the xdata to plot the error plots
drawnow();
c = get(b,'Children');
if iscell(c) && all(~cellfun(@isempty,c))
for i = 1:length(c)
xdata(:,i) = mean(get(c{i},'xdata'));
tempYData = get(c{i},'ydata');
ydata(:,i) = mean(tempYData(2:3,:))';
end
elseif length(b) > 1
for i = 1:length(b)
xdata(:,i) = b(i).XData+b(i).XOffset;
ydata(:,i) = b(i).YData;
end
else
xdata = mean(get(c,'xdata'));
tempYData = get(c,'ydata');
ydata = mean(tempYData(2:3,:))';
end

David J. Mack

### David J. Mack (view profile)

Works perfectly with previous ML builds. However, since the plotting change in R2013(?), BAR is not implemented as a patch object anymore and thus there are no children for the computation of the mean X position. This causes the error for grouped bars.

Wenxin Xu

### Wenxin Xu (view profile)

I have the same question with Naman... Thx!

Naman Deep Singh

Naman Deep Singh

### Naman Deep Singh (view profile)

Say i have a data matrix (d) of 3X4, and associated error matrix (e) of 3X4 . I want to make a stacked bars of all rows with error bars given in the error matrix. I would very obliged if you could illustrate the very case using an example.

Thanks ! :)

a

Right Grievous

### Right Grievous (view profile)

Really good piece of code. However, is it possible to colour one of the bars differently to the others (i.e as you would do using (set(h(1),'FaceColor','r') on a normal Matlab bar plot?

I have tried but it just colours them all the same...

Min Liu

### Min Liu (view profile)

Excellent tool. Thx!

Xiaohui Liu

### Xiaohui Liu (view profile)

Other error bars I find cannot deal with asymmetric error bounds. This one works like a charm.

Shatrughan

### Shatrughan (view profile)

Venn Ravichandran

### Venn Ravichandran (view profile)

x = 1:10; % x-axis
y = rand(10,2); % bar-heights
L = rand(10,2)*0.01; % Lower limit
U = rand(10,2)*0.03; % Upper limit
barSettings = {};
lineSettings = {U, 'linestyle', 'none'};
errorbarbar(x, y, L, barSettings, lineSettings);

sinoTrinity Liu

### sinoTrinity Liu (view profile)

"Hint: You can use the lineSettings variable to use distinct upper and lower limits."
Can you give me an example? I need to draw asymmetric error bounds. Thanks.

Shatrughan

### Shatrughan (view profile)

Thanks a lot !!

Venn Ravichandran

### Venn Ravichandran (view profile)

@Shatrughan: errorbarbar(1:5, rand(5,2)*10, rand(5,2),{'stacked'});

Shatrughan

### Shatrughan (view profile)

It is not as simple as you said. Would you mind putting up an illustrative example (especially for stacked bar plot). I would really appreciated it.

Thanks,
Shatrughan

### Updates

 22 Jun 2011 1.3 Error bars no longer have legends of their own. If you want them back on, then comment out line 63 :) 15 Jul 2009 1.2 Now works with stacked bars as well. 14 Jul 2009 1.1 Changed a bug which caused the program to give an error if either the barSettings or lineSettings was empty.
##### MATLAB Release
MATLAB 7.5 (R2007b)
##### Acknowledgements

Inspired: superbar

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.