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.

Example:

x = 1:10;

y = sin(x);

e = std(y)*ones(size(x));

herrorbar(x,y,e)

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

Jos (10584) (2021). HERRORBAR (https://www.mathworks.com/matlabcentral/fileexchange/3963-herrorbar), MATLAB Central File Exchange. Retrieved .

Created with
R13

Compatible with any release

- MATLAB > Graphics > 2-D and 3-D Plots > Line Plots >

**Inspired:**
errbar, RADIOMICS, errorbarxy, superbar, errorbar_x, Trimmed Spearman-Karber method.

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

Rikke PlougmannPaulina SenikoglouMatthew McIntyreJos (10584)@jacob save the file in a folder which you add to your path (more info? doc path) Usage is like any other function (read the Getting Started section of the manual!)

JacobTo use this function, do I simply put it into my current folder? Can I substitute "x,y,e" with my own variables or do I need to change mine to "x,y,e"? Thanks

Clareah nae bother figured it out

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

Joseph Zablockithank you

Samuel Mongeau-LachanceLuis GualcoDavid NielsenCommented line 148 so that it only plots the error bars on an already existing graph.

Jos (10584)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.

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

RYAN OCONNORJos (10584)@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.

Douglas BockHi 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?

Peter PabloFix for octave:

https://github.com/PeterPablo/matlab2tikz/commit/f8cea148bb0ffa8d02cf1d4ec4bb57d086612d4f

niu zexiJos (10584)@Patricia,

you can use UISTACK on the handles returned by HERRORBAR. Something along these lines might work for you:

h = errorbar(...)

uistack(h(2),'up')

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

PatriciaIs 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-?

Thanks!

CharlesThanks 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.

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

HughI 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?

HughJos (10584)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

ClaireI 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.

Thanks.

PearlFlorianPerfect!

GerryJos (10584)@Carlos, to avoid modifying the file, you can easily change the linestyles afterwards:

h = herrorbar(...) ;

set(h(2), 'linestyle', 'none') ;

CarlosTo remove the connecting lines just change

line 148 of the mfile to:

h = [h;plot(x,y,symbol,'LineStyle','none')];

DanNicely 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.

Jos (10584)@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'})

ngazakiMesses 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')

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

J BJos (10584)@ 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) ;

J BHas 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?

Jos (10584)@ J B. Please see my comment of 10 dec 2009:

h = errorbar(x,y,ex) ;

set(h,'linewidth',2)

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

Jos (10584)@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])

Andrew StephensonHow 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.

Thanks

JordanHow 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.

thanks

Kim HansenPLEASE PLEASE Help

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

errorbar(x,y,e,'LineStyle','none')

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

herrorbar(x,y,e,'LineStyle','none')

but that does not work.

Please help

philippe chassotJos (the author)Dear Aniruddha Sengupta, these incongruencies between errorbar and herrorbar are most likely due to version differences.

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

S IJosh HansonNice. Thank you

Anindo RoyGreat 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.

David SterlingNice 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.

Seth G.Works as described

Jos vdGTo 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).

Steven Shimizuvery 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.

Nadya VGood job! Thank you.

B LVery good. Thanks

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

C Lexcellent. i have been trying to do this for ages. thankyou.

Eugene TanWould 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.

Josh FGood stuff. You saved my thesis.

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

Ted RThis function works exactly as advertised. Use it!

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

Boh RuffinRem ZNot robust to log axes.

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

Bob Woodgood.

works like errorbar.

Stefan Schlampdavid wangvery good, you can use

errorbar(x,y,ex);

hold on;

herrorbar(x,y,ey);

to have both horizontal and verical error bars created.

Troy GoodsonI 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)

David Koski