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 .
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.
@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!)
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
ah nae bother figured it out
Anyone any idea how to plot in grey? Problems using rgb triplet ([0.5 0.5 0.5]) and linespec.
thank you
Commented line 148 so that it only plots the error bars on an already existing graph.
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.
My Matlab says that herrobar is an undefined function? It would be a pleasure when somebody would help me
@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.
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?
Fix for octave:
https://github.com/PeterPablo/matlab2tikz/commit/f8cea148bb0ffa8d02cf1d4ec4bb57d086612d4f
@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.
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!
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.
Great tool, did exactly what I wanted, no second guessing.
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?
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
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.
Perfect!
@Carlos, to avoid modifying the file, you can easily change the linestyles afterwards:
h = herrorbar(...) ;
set(h(2), 'linestyle', 'none') ;
To remove the connecting lines just change
line 148 of the mfile to:
h = [h;plot(x,y,symbol,'LineStyle','none')];
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.
@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'})
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')
It works great, but I have one question: How can I get bigger end markers?
@ 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) ;
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?
@ J B. Please see my comment of 10 dec 2009:
h = errorbar(x,y,ex) ;
set(h,'linewidth',2)
great, but can one change 'linewidth'?...or shall i give up trying!
@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
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
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
Dear Aniruddha Sengupta, these incongruencies between errorbar and herrorbar are most likely due to version differences.
Certain specifications, such as 'LineWidth', don't seem to work with "herrorbar" but work perfectly with "errorbar".
Nice. Thank you
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.
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.
Works as described
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).
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.
Good job! Thank you.
Very good. Thanks
Nice job, this is a very useful tool. Dont forget you can overlay graphs to get both x and y errorbars.
excellent. i have been trying to do this for ages. thankyou.
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.
Good stuff. You saved my thesis.
This rocks!! Use it with errorbar to plot both x and y error bars. I've had issues with "errorbarxy" function.
This function works exactly as advertised. Use it!
Right waht I need, it also saved me a lot of trouble.
Not robust to log axes.
its great, worked first time, thanx heaps, has saved me a whole lot of hassle
good.
works like errorbar.
very good, you can use
errorbar(x,y,ex);
hold on;
herrorbar(x,y,ey);
to have both horizontal and verical error bars created.
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)