如何绘制带有指示标准偏差的误差条的散点图

How to plot a scatter plot with error bars indicating standard deviation

我有一组数据 Y v/s X(~20k 数据点),绘制时是一个散点图。我想为 X 的范围绘制 Y 的误差线(例如,X 轴的长度为 100,然后我希望误差线代表每 10 个 X 单位的 Y 的标准偏差)

试一试:

N = 100;    % Number of points
n = 10;     % Number of x-bins

% Define and plot points
x = rand(N,1);
y = x.*rand(N,1);
scatter(x, y, '+');

% Define errorbar
bin = linspace(min(x), max(x), n+1);
ind = sum(bsxfun(@minus, x, ones(N,1)*bin)>=0,2);

m = NaN(n,1);
e = NaN(n,1);
for i = 1:n
    m(i) = mean(y(ind==i));   % Mean value over the bin
    e(i) = std(y(ind==i));    % Standard deviation
end

hold on
u = (bin(1:end-1)+bin(2:end))/2;
errorbar(u,m,e,'k');

希望对您有所帮助。