File Exchange

image thumbnail

Plot data with error bars on both x and y axes

version (3.58 KB) by Qi An
plot data with error bars along both x and y axes


Updated 07 Jan 2016

View Version History

View License

ERRORBARXY is a function to generate errorbars on both x and y axes
with specified errors modified from codes written by Nils Sjöberg
errorbarxy(x, y, lerrx, uerrx, lerry, uerry) plots the data with errorbars on both
x and y axes with error bars [x-lerrx, x+uerrx] and [y-lerry, y+uerry]. If there is
no error on one axis, set corresponding lower and upper bounds to [].
errorbarxy(x, y, errx, erry) plots the data with errorbars on both x and
y axes with error bars [x-errx, x+errx] and [y-erry, y+erry]. If there
is no error on one axis, set corresponding errors to [].
errorbarxy(..., S) plots data as well as errorbars using specified
character strings. S is a cell array of 3 element, {sData, cEBx, cEBy},
where sData specifies the format of main plot, cEBx specifies the
color of errorbars along x axis and cEBy specifies the color of
errorbars along y axis.
errorbarxy(AX,...) plots into AX instead of GCA.

H = errorbar(...) returns a vector of errorbarseries handles in H,
within which the first element is the handle to the main data plot and
the remaining elements are handles to the rest errorbars.
H is organized as follows:
H.hMain is the handle of the main plot
H.hErrorbar is a Nx6 matrix containing handles for all error bar lines,
where N is the number of samples. For each sample, 6
errorbar handles are saved in such an order:
[Horizontal bar, H bar left cap, H bar right cap,
Vertical bar, V bar lower cap, V bar upper cap]
For example
x = 1:10;
xe = 0.5*ones(size(x));
y = sin(x);
ye = std(y)*ones(size(x));
H=errorbarxy(x,y,xe,ye,{'ko-', 'b', 'r'});
draws symmetric error bars on both x and y axes.
NOTE: errorbars are excluded from legend display. If you need to
include errorbars in legend display, do the followings:
arrayfun(@(d) set(get(get(d,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','on'), H(2:end)); % include errorbars
'IconDisplayStyle','on'); % include errorbars in legend as a group.
legend('Main plot', 'Error bars');

Developed under Matlab version (R2010a)
Created by Qi An

Cite As

Qi An (2021). Plot data with error bars on both x and y axes (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (34)


Hiii, sorry to bother, it was simple ,,, modified your code a bit to have a control over the linewidth of the error bars


Lefteris Kosmidis

Marie Lu

nice code, thank you Qi An!

to change the size of the errorbars in the code, I edited l2 and l3 (horizontal caps) and l5 and l6 in the section %% plot data and errorbars

maxYrange = max(y)-min(y);
l2=line([lx(k) lx(k)],[y(k)-0.01*maxYrange y(k)+0.01*maxYrange]);

(for constant cap size relative to data range)

Nenad Bozinovic

Qi An

@Alejandro Another way is to calculate the start and end of all caps and use the handles provided in the output to change them. Something like: set(H.hErrorbar(1, 2), 'YData', [yy1, yy2]); Do the same for all caps and use a for loop if necessary.

Qi An

@Alejandro. An alternative way is to go into my codes and change the part where the lengths of all caps are defined.


Is there a way to make the end caps (the T's) be the same size for all? I'm passing several errorbarxy arguments and each have different errors, so in my final plot I have different sizes of end caps.


I figured out how to change the color of the error bar:

set(HMgO.hErrorbar,'Color',[0 0.45 0.74])

However, I do get an error:

"Error using
The name 'Color' is not an accessible property for
an instance of class

But what's funny is that it does work despite the error. The problem is that the error stops matlab from continuing to evaluate the rest of my script.

Qi An

@Michael use the handle provided in the output to change the linewidth and markersize. Something like set(H.hMain, 'linewidth', xx, 'markersize', yy)

Michael Lee

Hi just wondering how you change the linewidth and markersize of the error bars.Any help will be greatly appreciated.

Qi An

@ thomas Try something like: title('this is title');

thomas cecchetto

How can i modify plot's proprieties (like 'Title')?
i tried with hMain but i get this:

Error using graph2d.lineseries/set
The name 'Title' is not an accessible property for
an instance of class 'lineseries'.

Qi An

@ Sagar See your email for answers.


How do you change the linewidth or markersize for errorbars and the main plot?

thomas cecchetto

Hi, I and my physics lab group would like to thank you for helping us plotting the error bars exactly how our teacher wanted us to!You're our hero! XD

yoav kesselr

Chiara Giammanco

@ B.M.
The m file has a "hold off" at the end of its execution - so commenting it out would help. Or just remember to put the hold back on, as Qi An has in the example comment.


Qi An

@Felix Sorry for late reply. I haven't been monitoring this file lately. The input "x" is not optional. You have to set a vector for x. If you simply want x to have the same length of y, you can set x to be something like "x=1:length(y);" That should suffice the requirement.


Hi Qi An,

I have no errors on x but if I set it to [ ] it keeps saying:

Error using -
Matrix dimensions must agree.

Error in errorbarxy (line 132)

Error in TestArea (line 11)

It works well if there is something as xerror. Do you know why?

Qi An

Can you be more specific with your problem? Currently, the length of all caps are set to be 10% of the errors in the other axis, e.g. the cap along x-axis has a length of 10% of errors in y-axis.
You can either leave comments here or shoot me a line @

Caike Crepaldi

Hello Qi An,

That's a really nice function! I'm having some problems with the proportions of the 'T' marks at the end of the errorbars. Is there a way to change the size or control its proportions?


Qi An

@ B.M.

I don't have the problem you mentioned. I tried the example below and multiple plots can be drawn on the same plot:
x = 1:10;
xe = 0.5*ones(size(x));
y = sin(x);
ye = std(y)*ones(size(x));
H=errorbarxy(x,y,xe,ye,{'ko', 'b', 'r'});
hold on;
H=errorbarxy(x,y,xe,ye,{'ko', 'r', 'b'});
hold off;

If you still have problem, either leave comment here or email me @

As for the second question, see my example above again. If you use 'ko-' in the first element of the formatting string, data points are connected. Instead, if you use 'ko', data points are plotted in circles and isolated.


For some reason I'm not able to hold and draw multiple of these plots on the same figure :( Also I can't seem to specify (none) in S....always get my data points connected.

Qi An

Sebastian, you can use the handles of main plot/errorbars to customize your plot.

Qi An

Florian, great suggestion. I have changed my codes to allow customization of main plot using formatting string. See help for more details.

Florian Rückerl

Nice function!

you can change line 132 in the m-file from:
h=plot(x,y, 'color', color{1}); % main plot

This will plot the main data in the way you specify it in the first field of your color description.



will plot your data as black circles with red and blue error bars. If you want lines on top of that change 'ok' to '-ok'.

Take care,


Hello Qi An, how can i customize the plot? i do not want intersections between my data points.

Best Regards

Qi An

Eric, sorry for late response. Your suggestions are great. I have changed the output structure to make it more accessible. See help for more details.

Eric Brown

The handle output of this function would be more useful if it was a structure separating the XY plot references from the error bar references. Also, would be easier to edit x and y error bar properties if their handles were in a matrix format rather than a vector.

Qi An

You are right, Emily. I am an idiot. Fixed file uploaded.


This function is helpful for me, but I have a problem with the handles returned. The documentation suggests the first handle is the main data plot, but that one seems to be missing from the vector completely. Looks like it is overwritten after it is first created.

Qi An

Thanks for your comment, Mark.
I have fixed the first problem you mentioned. I was mixing the lower/upper bound of data with lower/upper bound of error. Now, the caps should be proportional to the length of error instead of the value of x or y. That's a good catch, Mark.
As for the second comment, I don't have to specify 'color' property in the plot function if it's the only parameter (I am using R2012b but it's the same since Matlab 6.0). However, I changed it anyway though it wouldn't change the behavior of the function.

Mark S.

Good function, but I had issues with the errorbar end caps being way too big. When I looked into the code, it seems that you calculate errx=(lx+ux)/2 on line 104 and likewise for erry on line 109. These error sizes then go into the length of the end caps. However, you actually define lx and ux etc as the lower and upper bounds, rather than the error. Thus in this case you should calculate errx as (lx-ux)/2 and so on. In your original code, the errorbar end caps quickly grow as the actual x and y values become large.

Also, there was an issue with applying color to the plots. In your code, you have plot(x,y,color), but at least in matlab 2012a, the plot function requires you to specify the 'Color' property before giving the color values.

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!