4.0

4.0 | 6 ratings Rate this file 231 Downloads (last 30 days) File Size: 3.32 KB File ID: #40221
image thumbnail

Plot data with error bars on both x and y axes

by

 

11 Feb 2013 (Updated )

plot data with error bars along both x and y axes

| Watch this File

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

Required Products MATLAB
MATLAB release MATLAB 8.0 (R2012b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (14)
25 Aug 2014 Qi An

@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

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?

Regards.

21 Feb 2014 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;
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.

14 Feb 2014 B.M.

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

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

04 Feb 2014 Qi An

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

22 Jan 2014 Florian Rückerl

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

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

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.

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

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

19 Sep 2013 Emily

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.

19 Aug 2013 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.

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.

Updates
12 Feb 2013

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

Excluded errorbars from legend display.

19 Aug 2013

Fixed a bug in errorbar cap display.

25 Sep 2013

Fixed a bug when outputting figure handles.

16 Jan 2014

Reorganize the output handle structure.

04 Feb 2014

Allow customization on main plot

28 Apr 2014

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

Contact us