Haren Shetty
Haren Shetty

Reputation: 53

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

I have a set of data Y v/s X (~20k data points) which when plotted are a scatter. I want to plot error bars for Y for a ranges of X(eg. the X axis is of length 100, then I want the errorbars to represent the standard deviation of Y for every 10 units of X)

Upvotes: 1

Views: 5330

Answers (1)

LowPolyCorgi
LowPolyCorgi

Reputation: 5188

Here is a try:

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');

Hope this helps.

Upvotes: 2

Related Questions