version 1.81.0.0 (3.58 KB) by
Qi An

plot data with error bars along both x and y axes

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

Qi An (2021). Plot data with error bars on both x and y axes (https://www.mathworks.com/matlabcentral/fileexchange/40221-plot-data-with-error-bars-on-both-x-and-y-axes), MATLAB Central File Exchange. Retrieved .

Created with
R2012b

Compatible with any release

**Inspired:**
errbar, errorbarxy, superbar, High-Throughput, Algorithmic Determination of Nanoparticle Structure from Electron Microscopy Images

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.

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

Thanks

Lefteris KosmidisMarie Lunice 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

e.g.

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

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

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

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 LeeHi 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 cecchettoHow 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.

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

thomas cecchettoHi, 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 kesselrChiara 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.

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

FelixHi 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?

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

Caike CrepaldiHello 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.

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.

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.

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

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

Florian RückerlNice 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

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

Best Regards

Qi AnEric, 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 BrownThe 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 AnYou are right, Emily. I am an idiot. Fixed file uploaded.

EmilyThis 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 AnThanks 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.