Code covered by the BSD License

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

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

4.36364
4.4 | 11 ratings Rate this file 144 Downloads (last 30 days) File Size: 3.58 KB File ID: #40221 Version: 1.81

# Plot data with error bars on both x and y axes

by

### Qi An (view profile)

11 Feb 2013 (Updated )

plot data with error bars along both x and y axes

File Information
Description

ERRORBARXY is a function to generate errorbars on both x and y axes
with specified errors modified from codes written by Nils Sjöberg
(http://www.mathworks.com/matlabcentral/fileexchange/5444-xyerrorbar)
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:
H=errorbarxy(...);
arrayfun(@(d) set(get(get(d,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','on'), H(2:end)); % include errorbars
hEB=hggroup;
set(H(2:end),'Parent',hEB);
set(get(get(hEB,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','on'); % include errorbars in legend as a group.
legend('Main plot', 'Error bars');

Developed under Matlab version 7.10.0.499 (R2010a)
Created by Qi An
anqi2000@gmail.com

Acknowledgements
Required Products MATLAB
MATLAB release MATLAB 8.0 (R2012b)
MATLAB Search Path
`/`
Tags for This File   Please login to tag files.
Comments and Ratings (30)
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

### Alejandro (view profile)

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

### Alejandro (view profile)

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 matlab.graphics.Graphics/set
The name 'Color' is not an accessible property for
an instance of class
'matlab.graphics.GraphicsPlaceholder'."

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

### Michael Lee (view profile)

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

### thomas cecchetto (view profile)

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

### thomas cecchetto (view profile)

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

### yoav kesselr (view profile)

25 Apr 2015 Chiara Giammanco

### Chiara Giammanco (view profile)

@ 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

### christec (view profile)

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)
lx=x-varargin{3};

Error in TestArea (line 11)
errorbarxy(X(:),Y(:),[],[],stdY(:),stdY(:))

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

Comment only
25 Aug 2014 Qi An

### Qi An (view profile)

@Caike
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 @ anqi2000@gmail.com

Comment only
23 Aug 2014 Caike Crepaldi

### Caike Crepaldi (view profile)

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?

Regards.

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;
y=cos(x);
H=errorbarxy(x,y,xe,ye,{'ko', 'r', 'b'});
hold off;

If you still have problem, either leave comment here or email me @ anqi2000@gmail.com.

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

### Florian Rückerl (view profile)

Nice function!

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

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

Example:

errorbarxy(x,y,errx,erry,{'ok','r','b'});

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,
Florian

20 Jan 2014 Sebastian

### Sebastian (view profile)

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

### Eric Brown (view profile)

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.

### Mark S. (view profile)

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