- 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 ?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (1개)
ag
2024년 7월 16일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Errorbars에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!