- File exchange page: https://www.mathworks.com/matlabcentral/fileexchange/55222-xbinavg-x-y-xedge-medopt?s_tid=answers_rc2-1_p4_Topic
- errorbar: https://www.mathworks.com/help/matlab/ref/errorbar.html
X, Y plot with errorbars on the same figure ?
2 views (last 30 days)
Show older comments
I would like to plot errorbars on a X,Y plot. Errorbars should be computed based on mean and standard deviation for X data (X data first must be grouped in bins in order mean and standard deviation be computed). I have tried many command but it is no use.
Could you please help me?
0 Comments
Answers (1)
ag
on 16 Jul 2024
Hi Ivan,
I understand that you want to plot error bars along with the original plot.
The below code plots X, Y data along with errorbars, where Standard deviation of Y is used as the error lenght.
function [meanv, stdv, xv, nv] = xbinavg(x, y, xedge)
% Averaging y-data with x-data bin
% # Input
% x: xdata
% y: ydata corresponding to x
% xedge: bin-edges
%
% # Output
% meanv: mean y-value in each x-bin
% stdv : standard deviation of y
% xv : center values of bin-edges for plot
% nv : the number of data in each x-bin
x = x(:);
y = y(:);
for i = 1:length(xedge)-1
doil = (x>=xedge(i));
doiu = (x<xedge(i+1));
% output
xv(i) = mean([xedge(i) xedge(i+1)]);
stdv(i) = std(y(find(doil.*doiu)));
nv(i) = length(y(find(doil.*doiu)));
meanv(i) = mean(y(find(doil.*doiu)));
end
end
%creating random X and Y data
x = rand(1,200)*100;
y = x.^2 + 5000*randn(size(x));
[yMean, yStdDeviation, xBinCenter, binSize] = xbinavg(x, y, 0:10:100);
plot(x,y);
hold on;
errorbar(xBinCenter, yMean,yStdDeviation,'k','LineWidth', 1.5);
hold off;
For more details, please refer to the following pages:
Hope this helps!
0 Comments
See Also
Categories
Find more on Errorbars in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!