4.84615
4.8 | 27 ratings Rate this file 363 Downloads (last 30 days) File Size: 5.92 KB File ID: #30639
image thumbnail

barwitherr(errors,varargin)

by

 

04 Mar 2011 (Updated )

This is a very simple extension of MatLab's bar function to add error bars.

| Watch this File

File Information
Description

This acts in the same way as MatLab's bar function but with an additional first parameter containing the errors on the y values.
The y values are plotted using MatLab's bar function.
The x values are determined from the bar plot and used with MatLab's errorbar function to add errorbars to the bar chart that are centred on the column.
Adding an extra dimension to the errors allows for asymmetric errors.
Now also compatible with new graphics introduced in 2014b

MATLAB release MATLAB 7.11 (R2010b)
MATLAB Search Path
/
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (52)
26 Mar 2015 James Rankin

>> Now updated for 2014b - thanks Jiri for the linke.

I had to also add a drawnow after line 115:
handles.bar = bar(varargin{:});

This is a bit weird, but the handles.bar wasn't properly initialised (XOffset used later would be 0) until the plot command had been allowed to execute...

Incidentally this was on Ubuntu 14.04.

Thanks for a useful function Martina!

Comment only
24 Mar 2015 Jatin Gera  
12 Mar 2015 Martina Callaghan

Hi Eric,

I would suggest simply generating the plot and then applying a grayscale colour map. That's how I convert my figures for papers.

(>> colormap gray)

Martina

Comment only
10 Mar 2015 Eric

Eric (view profile)

Hi Martina - still an excellent function (I'm using R2013b) and thank you for updating it with my earlier suggested edit.

I'm trying to prepare a bar chart in black & white as required by a journal, and the solution here

http://blogs.mathworks.com/pick/2011/07/15/creating-hatched-patches/

reports an error:

Error using specgraph.barseries/get
The name 'paperpositionmode' is not an accessible property for an
instance of class 'barseries'.

Error in applyhatch_plusC (line 82)
oldppmode = get(h,'paperpositionmode');

...which I presume arises because of the way barwitherr generates the plot associated with the handle h.

I know it's not a fault in your code, but do you have any ideas how we could replace a colour with a hatching or shading pattern?

Thanks

17 Dec 2014 Wasim

Wasim (view profile)

Any chance you could add an option to control the errorbar tick width, as this code used to do before Matlab 2014b kicked in:
http://www.mathworks.com/matlabcentral/fileexchange/22826-adjust-error-bar-width/content/errorbar_tick.m
Thanks!

16 Dec 2014 Barbara

Barbara (view profile)

 
29 Oct 2014 Martina Callaghan

Now updated for 2014b - thanks Jiri for the linke.

Comment only
28 Oct 2014 Jiri

Jiri (view profile)

The fix for 2014b is here:
http://www.mathworks.com/matlabcentral/answers/158899-barweb-in-2014b-doesn-t-work-any-more

Love it!

28 Oct 2014 Francesco

As mentioned by others, error bars don't show properly on the new 2014b...

27 Oct 2014 Carl

Carl (view profile)

Great submission - very useful! The instructions are pretty clear as well.

17 Sep 2014 Martina Callaghan

Hi WH,

I do not know why it wouldn't work in 2014b. Something similar was reported for 2013b though it worked in previous versions and in 2014a. I do not use 2014b so haven't tested. Perhaps a Matlab issue that will be ironed out.

Comment only
17 Sep 2014 WH

WH (view profile)

This is a very useful script. Thank you!
However, I can get it to work in Matlab 2014a, but it doesn't work in Matlab 2014b trial version. The bars display properly but the error bars didn't. Do you know what the problem is? Thank you!

16 Sep 2014 Parto

Parto (view profile)

Thanks for your quick response. The color of both bars change together to red or green.
for example:
y=[0.33,0.362];
errY=[0.017,0.054];
h=barwitherr(errY,y);
set(h(1),'FaceColor','g')

Comment only
16 Sep 2014 Martina Callaghan

Hi Parto,

For example:
h = barwitherr(randn(3,4), randn(3,4));
set(h(1), 'FaceColor','r')
set(h(2), 'FaceColor','g')

The first parameter will be red and the second parameter will be green.

Comment only
15 Sep 2014 Parto

Parto (view profile)

Thank you very much for this useful matlab file. I just wanted to know how I can modify each bar separately, for example color?

Comment only
23 Jul 2014 Elizabeth

Thank you! Works a charm!

13 May 2014 Feifei Zhou  
11 Apr 2014 Jaakko

Jaakko (view profile)

Nice and clean output. But, something fishy going on with Matlab R2013b. Even the example

y = randn(3,4);
errY = 0.1.*y;
h = barwitherr(errY, y);

spits out error

Error using errorbar (line 74)
X, Y and error bars must all be the same length

Error in barwitherr (line 126)
hErrorbar(col) = errorbar(mean(x,1), values(xOrder,col), lowerErrors(xOrder,col),
upperErrors(xOrder, col), '.k');

With earlier releases barwitherr.m seemed to work fine, and the very latest R2014a works as well, so probably some minor changes/bugs in Matlab internal functions..

28 Feb 2014 Efren

Efren (view profile)

Thank you very much! This is a very good extra. It is nice from you :)

12 Feb 2014 Martina Callaghan

Hi Clare, yes, you can change the line width via:
[hBox hErr] = barwitherr(errY, y);
set(hErr(:), 'LineWidth', 2)

Comment only
12 Feb 2014 Clare

Clare (view profile)

Very useful submission.
Is it possible to change the line width of the error bars?

09 Feb 2014 Annick

Annick (view profile)

Just got it to do it with the plot editor. all good.

Comment only
09 Feb 2014 Annick

Annick (view profile)

Really helpful function, thanks! Is there any way to get it to plot a horizontal bar graph? I tried using the "rotate" function but it comes up the error "H must contain axes children only".

Thanks!

20 Jan 2014 Martina Callaghan

Hi Cui, use asymmetric error bars (example in header) and set one error to 0.

Comment only
20 Jan 2014 Cui

Cui (view profile)

I only want to plot the above half errorbar, how can I do with this function? Thanks very much.

Comment only
01 Nov 2013 Beate

Beate (view profile)

 
25 Sep 2013 Matt

Matt (view profile)

Fantastic.

18 Jul 2013 Charles Collin

Very nice function. I changed the last bit (starting at line 107) like so to get rid of the dot in the middle of the error bar.

if nRows > 1
for col = 1:nCols
% Extract the x location data needed for the errorbar plots:
x = get(get(handles.bar(col),'children'),'xdata');
% Use the mean x values to call the standard errorbar fn; the
% errorbars will now be centred on each bar; these are in ascending
% order so use xOrder to ensure y values and errors are too:
ebhandle = errorbar(mean(x,1),values(xOrder,col),lowerErrors(xOrder,col), upperErrors(xOrder, col), '.k');
set(ebhandle, 'Marker', 'none');

end
else
x = get(get(handles.bar,'children'),'xdata');
ebhandle = errorbar(mean(x,1),values,errors,'.k');
set(ebhandle, 'Marker', 'none');

end

Comment only
12 Jul 2013 Cherkasov Nikolay

Unfortunately it does not work when one deals with the only group. Even the modified built-in example does not work ( y = randn(1,4); errY = zeros(1,4,2); errY(:,:,1) = 0.1.*y; errY(:,:,2) = 0.2.*y; barwitherr(errY, y); )
In order to solve this problem, line 118 should be modified to provide errors more explicitly - like this
errorbar(mean(x,1),values,values-errors(:,:,1),errors(:,:,2)-values,'.k')

Comment only
12 Jul 2013 Cherkasov Nikolay

Unfortunately it does not work when one deals with the only group. Even the modified built-in example does not work ( y = randn(1,4); errY = zeros(1,4,2); errY(:,:,1) = 0.1.*y; errY(:,:,2) = 0.2.*y; barwitherr(errY, y); )
In order to solve this problem, line 118 should be modified to provide errors more explicitly - like this
errorbar(mean(x,1),values,errors(:,:,1),errors(:,:,2),'.k')

24 Jun 2013 Martina Callaghan

Mossi, see the help entry for the file and the help entry for bar (dictates what kind of plots can be created).

You appear to have four parameters in three conditions. So, your data (y) and errors (errY) should each be held in a 3x4 matrix.
h = barwitherr(errY, y);

Use the handles "h" to tailor figure properties/appearance.

Comment only
24 Jun 2013 Mossi

Mossi (view profile)

Thank you so much for the useful function. I could not create a special plot though. Here is the link to the plot picture:

https://www.dropbox.com/s/jn45qasmr8zec8a/Photo%202013-06-24%201%2024%2005%20AM.png

I have created this plot in Excel but could not use Matlab to do it. Could you please guide me on how to use your function for plotting this?

Comment only
14 Jun 2013 Eric

Eric (view profile)

Very useful function - I added a line to return the handle to the plot, so that existing commands from Matlab's "bar" can still be used with barwitherr:

After
>handles.bar = bar(varargin{:}); % standard implementation of bar fn
>hold on

Add:
h = handles.bar

and then change the function line at the top of the program to:

function h = barwitherr(errors,varargin)

You can then use things like

set(h(1),'FaceColor','g');

in your code that calls barwitherr, exactly as you would with calls to 'bar'.

05 Jun 2013 Stratos  
10 May 2013 Brett

Brett (view profile)

Thanks for the program. Aurélien, nice addition, I modified your code a little to allow for different colors for multiple parameters in each group:

edge = [.4 .4 .4;.4 .4 .4;.4 .4 .4];
face = [0 1 0;0 0 1;1 0 0];
for n=1:length(handles.bar) %set colors
set(handles.bar(n),'edgecolor',edge(n,:),'facecolor',face(n,:));
end

Comment only
10 May 2013 Aurélien BERTIN

I've added the following nice things to change color and bar size :
After line "handles.bar = bar(varargin{:}); % standard implementation of bar fn":
for n=1:length(handles.bar)
set(handles.bar(n),'edgecolor',[.4 .4 .4],'facecolor',[1 0.6 0.2]);
end
Colors can be even changed for a specified n.

After "hold off":
for n=1:length(h)
clear hh
hh=get(h(n),'children');
set(hh(2),'linewidth',1.5)
x = get(hh(2),'xdata');
w = .1; % Horizontal lines width
x(4:9:end) = x(1:9:end)-w/2;
x(7:9:end) = x(1:9:end)-w/2;
x(5:9:end) = x(1:9:end)+w/2;
x(8:9:end) = x(1:9:end)+w/2;
set(hh(2),'xdata',x(:))
end

It changes the width, and the aspect of the error bar line. With tha, you should also add an "end" after, the hold off being non longer the termination.

Anyway, very nice program!

27 Apr 2013 Laura

Laura (view profile)

 
26 Mar 2013 Armita

Armita (view profile)

 
19 Mar 2013 Alexandre  
09 Oct 2012 Brendan Floyd

Simple and works well. Nice job.

04 May 2012 Don Vaughn

Simple. Quick. Easy. Exactly what I was looking for

04 May 2012 Alex

Alex (view profile)

Works great for me, thanks!

16 Nov 2011 Martina Callaghan

Asymmetric errors on 1D/2D data now working.

Comment only
11 Nov 2011 Jens

Jens (view profile)

The asymmetric errors seem to work only with grouped/2D data

Comment only
28 Oct 2011 Martina Callaghan

The other options I came across were barerror #9377 & barweb #10803 as noted earlier. They were too complex for what I wanted. This submission is not an extension of any other hence no reference. It is something I find useful and have submitted in case others would too. If not of use, no problem.

Comment only
28 Oct 2011 Bolu Ajiboye

Bolu Ajiboye (view profile)

You stated that you came across other error bar scripts (like barweb, which has been around at least 6 years) but completely failed to acknowledge them.

Comment only
24 Oct 2011 Martina Callaghan

I have just gotten around to updating this so that it can be called with asymmetric error bars. Add an extra dimension to the erros so as to specify the upper and lower in each. Hope this helps.

Comment only
19 Oct 2011 sinoTrinity Liu

The errors are symmetric. How can I add asymmetric errors?

Comment only
25 May 2011 Quoc Viet Hung Nguyen  
09 Mar 2011 Martina Callaghan

The ones I came across (barerror #9377 & barweb #10803) are much more complex. With complexity came the need for extra parameters and less user control.
This submission is just a basic extension of MatLab's bar and errorbar functions, which are cited!
I don't have the time to go through all of the other versions to compare. If you do, feel free to post how you get on.

Comment only
08 Mar 2011 Zachary Danziger

Sorry, maybe its easier to search for files with file names instead of file IDs:

24718-bar-with-errorbars
27387-create-healthy-looking-error-bars
25613-barrerror-utility-for-bar-plots-with-automatic-error-bars
9377-barerror
9541-barerrorbar

Comment only
08 Mar 2011 Zachary Danziger

What is the relevant difference between this code and submission #24718 specifically (or generally submissions #27387, #25613, #9377, or #9541)?

This is not a critique specific to this code, which is probably fine, but is there some way to reach a consensus on which of these functions is best? Or perhaps merge these submissions, or create a new umbrella submission for all of these? At any rate, the author should cite some of these predecessors.

Comment only
Updates
12 Aug 2011

Now accounts for the fact that if x-values are also supplied, they may not be in ascending order.

24 Oct 2011

This version allows for asymmetric errors as requested.

15 Nov 2011

Now allows asymmetric errors on 1D data.

14 Jan 2013

Bug fix: xOrder should be specified when second argument for standard bar function is a width/property.

14 Jun 2013

Returns handle as recommended by Eric in submission comments

08 Jul 2013

Only return handle if requested.

18 Jul 2013

Recent comments addressed via bug fix, appearance update and extra optional output parameter.

27 Aug 2013

Errors always defined in terms of upper and lower, even if symmetric.

29 Oct 2014

Added compatibility with 2014b graphics

Contact us