View License

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

» Watch video

Highlights from

4.2 | 42 ratings Rate this file 647 Downloads (last 30 days) File Size: 2.55 KB File ID: #3963 Version: 1.0
image thumbnail



Jos (10584) (view profile)


16 Sep 2003 (Updated )

Horizontal errorbar plot.

| Watch this File

File Information

HERRORBAR Horizontal Error bar plot.
   HERRORBAR(X,Y,L,R) plots the graph of vector X vs. vector Y with horizontal error bars specified by the vectors L and R. L and R contain the left and right error ranges for each point in X. Each error bar is L(i) + R(i) long and is drawn a distance of L(i) to the right and R(i) to the right the points in (X,Y). The vectors X,Y,L and R must all be the same length. If X,Y,L and R are matrices then each column produces a separate line.
   HERRORBAR(X,Y,E) or HERRORBAR(Y,E) plots X with error bars [X-E X+E].
   HERRORBAR(...,'LineSpec') uses the color and linestyle specified by the string 'LineSpec'. See PLOT for possibilities.
      x = 1:10;
      y = sin(x);
      e = std(y)*ones(size(x));

History notice:
This code was originally posted to the newsgroup, comp.soft-sys.matlab, by me. In 2003 Greg Aloe (The MathWorks) rightfully decided to take ownership, after several unsuccesfull attempts to contact me, and put it on the FEX so that others could benefit from it.
In 2006 I contacted Greg Aloe and we decided to return ownership to me.
I like to thank him for his efforts. Jos


This file inspired Trimmed Spearman Karber Method., Errorbar X, Radiomics, Errbar, Errorbarxy, and Superbar.

MATLAB release MATLAB 6.5 (R13)
Other requirements This function should work with at least MATLAB 5.2 (R10) and later.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (66)
16 Feb 2017 Clare

Clare (view profile)

ah nae bother figured it out

Comment only
16 Feb 2017 Clare

Clare (view profile)

Anyone any idea how to plot in grey? Problems using rgb triplet ([0.5 0.5 0.5]) and linespec.

12 Feb 2017 Joseph Zablocki

thank you

28 Jan 2017 Samuel Mongeau-Lachance

09 Jan 2017 Luis Gualco

06 Jan 2017 David Nielsen

Commented line 148 so that it only plots the error bars on an already existing graph.

20 May 2016 Jos (10584)

Jos (10584) (view profile)

Download the function, put it in a folder and add the folder to the matlab path (if you have not done so before). Please read the help how to do this.

Comment only
20 May 2016 Ohibsky

My Matlab says that herrobar is an undefined function? It would be a pleasure when somebody would help me

Comment only
06 Apr 2016 RYAN OCONNOR

01 Oct 2015 Jos (10584)

Jos (10584) (view profile)

@Douglas The width of the tees (arms of the error bars) are determined by range of the y-values. You may want to tweak line 103 of this (old) function.

Comment only
27 Sep 2015 Douglas Bock

Hi Jos, great file. However the herrorbar bar arms get stretched when the range of the horizontal axis is specified. Has anyone got any ideas about how to fix this?

11 May 2015 Peter Pablo

Fix for octave:

Comment only
05 Apr 2015 niu zexi

25 Oct 2014 Jos (10584)

Jos (10584) (view profile)

you can use UISTACK on the handles returned by HERRORBAR. Something along these lines might work for you:
h = errorbar(...)

With these handles you can also change the properties of the plot symbols and the error bars.

Comment only
20 Oct 2014 Patricia

Is it possible to either change the order in which the errorbar and marker are plotted? Or is there any other fix that would remove the line that is drawn across the marker, so I can have something like -o-?

15 Oct 2014 Charles

Thanks a lot, really good tool. Had a little bit of trouble figuring out how to change the marker style, color, etc because of the plotting scheme, but I got there eventually.

@Hugh, to remove the vertical lines at either end simply change the "tee" variable to 0 in line 103.

28 Feb 2014 Stan

Stan (view profile)

Great tool, did exactly what I wanted, no second guessing.

26 Nov 2013 Hugh

Hugh (view profile)

I am looking to plot a lot of these herrorbars together on one graph, to facilitate this is it possible to reduce the circle size, and also to reduce or even remove the vertical lines at the end of each arm? (Also is it possible to have other markers for the midpoint other than a circle?

Comment only
26 Nov 2013 Hugh

Hugh (view profile)

27 Aug 2013 Jos (10584)

Jos (10584) (view profile)

The length of the vertical line (tee) is determined by the data. You can enforce a minimal tee length by modifying the code a little: add
tee = max(tee,0.2) ;
after line #103 of herrorbar.m

Comment only
15 Aug 2013 Claire

Claire (view profile)

I am having problems getting the lines at the end of the error bars (like this |--| , right now it's just plotting like this -- ).
Does anyone know how to turn on the end lines? I can't find it in the code anywhere.

Comment only
03 Jun 2013 Pearl

Pearl (view profile)

04 Mar 2013 Florian


28 Mar 2011 Gerry

Gerry (view profile)

12 Mar 2011 Jos (10584)

Jos (10584) (view profile)

@Carlos, to avoid modifying the file, you can easily change the linestyles afterwards:
h = herrorbar(...) ;
set(h(2), 'linestyle', 'none') ;

Comment only
12 Mar 2011 Carlos

Carlos (view profile)

To remove the connecting lines just change
line 148 of the mfile to:
h = [h;plot(x,y,symbol,'LineStyle','none')];

Comment only
04 Mar 2011 Dan

Dan (view profile)

Nicely done - though I concur with previous problems concerning the height of tee for single points (or multiple points at the same y-value). My work around was to allow y to be longer than x, then clip y to y([1 : length(x)]), but this won't work for non-vector input.

20 Oct 2010 Jos (10584)

Jos (10584) (view profile)

@Juha, it is ill-advised to use legend that way. Better get the indices of the plots and use them to create a proper legend.
Also note that this function was written for ML13, and therefore returns 2 handles instead of 1. Use this to your advantage!

h1 = herrorbar(22, 20, 2.74, 'o')
hold on;
h2 = herrorbar(21, 26, 2.63, 'p')
legend([h1(2) h2(2) h2(1)],{'test1', 'test2','error of test2'})

Comment only
19 Oct 2010 ngazaki

Messes legend if you use different markers and legend. Try this example and you'll know what I mean:

herrorbar(22, 20, 2.74, 'o')
hold on;
herrorbar(21, 26, 2.63, 'p')
legend('test1', 'test2')

26 Apr 2010 Shirley

It works great, but I have one question: How can I get bigger end markers?

Comment only
23 Apr 2010 J B

J B (view profile)

21 Apr 2010 Jos (10584)

Jos (10584) (view profile)

@ J B
You can modify the two handles returned by herrorbar separately, as in:

h = errorbar(x,y,ex) ;
set(h(1),'color','r') ;
set(h(2),'linewidth',2) ;

Comment only
21 Apr 2010 J B

J B (view profile)

Has anyone had any success when using the herrorbar function of plotting thin errorbars (i.e. linewidth = 1) yet with a thick line (i.e. linewidth =2) conecting plotted points?

Comment only
29 Jan 2010 Jos (10584)

Jos (10584) (view profile)

@ J B. Please see my comment of 10 dec 2009:

h = errorbar(x,y,ex) ;

Comment only
28 Jan 2010 J B

J B (view profile)

great, but can one change 'linewidth'?...or shall i give up trying!

Comment only
10 Dec 2009 Jos (10584)

Jos (10584) (view profile)

@Jordan, @ Andrew
Note that HERRORBAR is based on errorbar R13. As a work around you can get the handle:

h = herrorbar(x,y,ex) ;

and modify its properties using SET. For instance,

set(h,'color',[0.5 0.7 1])

Comment only
09 Dec 2009 Andrew Stephenson

How do I go about changing the colour to something other than the default 'k, r, g, b' values? When using the errorbar command I can simply type

erroarbar(x,y,ex,'.', 'color',[0.5, 0.7, 1])

but this doesn't work when using herrorbar.
Any help would be greatly appreciated.


05 May 2009 Jordan

Jordan (view profile)

How do I put vertical end markers on my horizontal error bars? The code worked great to put them in but I have data points that overlap substantially so I need end markers to show their bounds.


Comment only
21 Nov 2008 Kim Hansen


I am almost about to give up. How do I remove those crappy connection lines that herrorbar produces. In the normal errorbar i can just write


where e offcourse is the error on the y values which in my case should be added to the x values. So I really want the command to be like this


but that does not work.
Please help

Comment only
19 Oct 2008 philippe chassot

10 Sep 2008 Jos (the author)

Dear Aniruddha Sengupta, these incongruencies between errorbar and herrorbar are most likely due to version differences.

Comment only
01 Sep 2008 Aniruddha Sengupta

Certain specifications, such as 'LineWidth', don't seem to work with "herrorbar" but work perfectly with "errorbar".

Comment only
16 Nov 2007 S I

05 Mar 2007 Josh Hanson

Nice. Thank you

28 Sep 2006 Anindo Roy

Great code. I had a question: is there any MATLAB code out there which allows one to errorbars on a bar plot (like Excel does)?
Thanks in advance for your suggestions.

Comment only
21 Sep 2006 David Sterling

Nice work. Currently won't put an error bar on a single point though... How about changing line 103 to be something like:

tee = max(0.01, (max(y(:))-min(y(:)))/100);

so that there is a default tee height for single points. More generally you might set default tee height to some function of Ylim so error bars show up no matter the Y scale.

31 Aug 2006 Seth G.

Works as described

23 Aug 2006 Jos vdG

To Steven Shimizu:
thanks for your rating. This submission is based on the errorbar function in R10, which does not expand scalar inputs. This feature has only been introduced in more recent releases (>R13).

Comment only
18 Aug 2006 Steven Shimizu

very good, except you can't just enter the value of the error, (i.e. herrorbar(x,y,0.1), unlike with errorbar. Instead, you have to create a vector of all the same values.

20 Jun 2006 Nadya V

Good job! Thank you.

06 Jun 2006 B L

Very good. Thanks

23 Sep 2005 David Swa

Nice job, this is a very useful tool. Dont forget you can overlay graphs to get both x and y errorbars.

15 Sep 2005 C L

excellent. i have been trying to do this for ages. thankyou.

25 Jul 2005 Eugene Tan

Would like to recommend a change in the code

tee = (max(x(:))-min(x(:)))/100;

changed to using some average of the given error:

tee = mean([u; l])/3;

This will then accomodate single points with errors. You can do the same in the for Matlab's "errorbar" function.

24 May 2005 Josh F

Good stuff. You saved my thesis.

18 May 2005 Rebecca Vislay-Meltzer

This rocks!! Use it with errorbar to plot both x and y error bars. I've had issues with "errorbarxy" function.

22 Feb 2005 Ted R

This function works exactly as advertised. Use it!

14 Feb 2005 Carlos Vargas

Right waht I need, it also saved me a lot of trouble.

12 Jan 2005 Boh Ruffin

29 Nov 2004 Rem Z

Not robust to log axes.

22 Nov 2004 Jonathan Ashmore

its great, worked first time, thanx heaps, has saved me a whole lot of hassle

26 Apr 2004 Bob Wood

works like errorbar.

28 Jan 2004 Stefan Schlamp

06 Jan 2004 david wang

very good, you can use
hold on;
to have both horizontal and verical error bars created.

28 Oct 2003 Troy Goodson

I looked at a few of the errorbar functions here. I really like that this is based on the included errorbar.m function as I don't have to learn new input arguments. The only things I think are missing are

- an option to do both horizontal and vertical error bars.

- an option to change the size of the tees on the error bars (MathWorks' errorbar.m doesn't have this, either)

25 Oct 2003 David Koski

Contact us