View License

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

» Watch video

Highlights from
Plot data with error bars on both x and y axes

4.4 | 12 ratings Rate this file 150 Downloads (last 30 days) File Size: 3.58 KB File ID: #40221 Version: 1.81
image thumbnail

Plot data with error bars on both x and y axes


Qi An (view profile)


11 Feb 2013 (Updated )

plot data with error bars along both x and y axes

| Watch this File

File Information

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


This file inspired High Throughput, Algorithmic Determination Of Nanoparticle Structure From Electron Microscopy Images, Errbar, Errorbarxy, and Superbar.

Required Products MATLAB
MATLAB release MATLAB 8.0 (R2012b)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (32)
03 Feb 2017 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)

Comment only
12 Sep 2016 Nenad Bozinovic

05 Jul 2016 Qi An

Qi An (view profile)

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

Comment only
05 Jul 2016 Qi An

Qi An (view profile)

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

Comment only
30 Jun 2016 Alejandro

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.

Comment only
01 Jun 2016 Alejandro

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.

Comment only
01 May 2016 Qi An

Qi An (view profile)

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

Comment only
28 Apr 2016 Michael Lee

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

Comment only
15 Apr 2016 Qi An

Qi An (view profile)

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

Comment only
14 Apr 2016 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'.

Comment only
07 Apr 2016 Qi An

Qi An (view profile)

@ Sagar See your email for answers.

Comment only
06 Apr 2016 Sagar

Sagar (view profile)

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

05 Apr 2016 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

27 Oct 2015 yoav kesselr

25 Apr 2015 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.

28 Mar 2015 christec

06 Feb 2015 Qi An

Qi An (view profile)

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

Comment only
14 Jan 2015 Felix

Felix (view profile)

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?

Comment only
25 Aug 2014 Qi An

Qi An (view profile)

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 @

Comment only
23 Aug 2014 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?


21 Feb 2014 Qi An

Qi An (view profile)

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

Comment only
14 Feb 2014 B.M.

B.M. (view profile)

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.

04 Feb 2014 Qi An

Qi An (view profile)

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

Comment only
04 Feb 2014 Qi An

Qi An (view profile)

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

Comment only
22 Jan 2014 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,

20 Jan 2014 Sebastian

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

Best Regards

16 Jan 2014 Qi An

Qi An (view profile)

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.

Comment only
02 Oct 2013 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.

24 Sep 2013 Qi An

Qi An (view profile)

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

Comment only
19 Sep 2013 Emily

Emily (view profile)

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.

Comment only
19 Aug 2013 Qi An

Qi An (view profile)

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.

Comment only
15 Aug 2013 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.

12 Feb 2013 1.1

Added support to plot on specified axes; Added support to specify color of plots and errorbars; Output avector of errbar series handles; Fixed a couple of minor bugs.

13 Feb 2013 1.2

Excluded errorbars from legend display.

19 Aug 2013 1.3

Fixed a bug in errorbar cap display.

25 Sep 2013 1.4

Fixed a bug when outputting figure handles.

16 Jan 2014 1.5

Reorganize the output handle structure.

04 Feb 2014 1.6

Allow customization on main plot

28 Apr 2014 1.7

Check for formatting string. Fixed a bug when specifying axis to plot.

23 Nov 2015 1.8

Fixed a bug when some of errx/erry/lerrx/uerrx/lerry/uerry are not specified the code will error out.

07 Jan 2016 1.81

The code will "remember" if the axis is set to be "HOLD ON".

07 Jan 2016 1.81

The code will "remember" if the axis is set to be "HOLD ON".

Contact us