Borrar filtros
Borrar filtros

X, Y plot with errorbars on the same figure ?

8 visualizaciones (últimos 30 días)
Ivan Mich
Ivan Mich el 24 de Feb. de 2021
Respondida: ag el 16 de Jul. de 2024 a las 16:31
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?

Respuestas (1)

ag
ag el 16 de Jul. de 2024 a las 16:31
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!

Categorías

Más información sobre Errorbars en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by