File Exchange

image thumbnail

HERRORBAR

version 1.0 (2.55 KB) by

Horizontal errorbar plot.

4.23404
45 Ratings

172 Downloads

Updated

View License

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

Comments and Ratings (71)

Jos (10584)

Jos (10584) (view profile)

@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!)

Jacob

Jacob (view profile)

To 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

Clare

Clare (view profile)

ah nae bother figured it out

Clare

Clare (view profile)

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

thank you

Luis Gualco

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

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.

Ohibsky

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

RYAN OCONNOR

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.

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?

niu zexi

Jos (10584)

Jos (10584) (view profile)

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

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-?
Thanks!

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.

Stan

Stan (view profile)

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

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?

Hugh

Hugh (view profile)

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

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

Pearl

Pearl (view profile)

Florian

Perfect!

Gerry

Gerry (view profile)

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

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')];

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.

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

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

Shirley

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

J B

J B (view profile)

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) ;

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?

Jos (10584)

Jos (10584) (view profile)

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

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

J B

J B (view profile)

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

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])

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.

Thanks

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.

thanks

Kim Hansen

PLEASE 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 chassot

Jos (the author)

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

Aniruddha Sengupta

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

S I

Josh Hanson

Nice. Thank you

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.

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.

Seth G.

Works as described

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

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.

Nadya V

Good job! Thank you.

B L

Very good. Thanks

David Swa

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

C L

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

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.

Josh F

Good stuff. You saved my thesis.

Rebecca Vislay-Meltzer

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

Ted R

This function works exactly as advertised. Use it!

Carlos Vargas

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

Boh Ruffin

Rem Z

Not robust to log axes.

Jonathan Ashmore

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

Bob Wood

good.
works like errorbar.

Stefan Schlamp

david wang

very good, you can use
errorbar(x,y,ex);
hold on;
herrorbar(x,y,ey);
to have both horizontal and verical error bars created.

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)

David Koski

MATLAB Release
MATLAB 6.5 (R13)

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

» Watch video