1

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)

Haren Shetty
  • 53
  • 1
  • 8
  • 2
    Something like that ? : http://stackoverflow.com/questions/7879449/how-to-plot-an-error-bar-plot-with-standard-deviation-values-in-matlab? – Hoki Jan 30 '15 at 21:58

1 Answers1

2

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.

Ratbert
  • 5,463
  • 2
  • 18
  • 37
  • Thanks a lot! This is exactly what i needed. Could you modify the same code for the error plots to indicate a 95% confidence interval and now the x axis is in log. So the error plots would be for every 10 powers.(i.e 2 errorbars for N=100, 3 errorbars for N=1000, etc) – Haren Shetty Feb 06 '15 at 20:19
  • 1
    But for the log spacing, you can simply use the `logspace` instead of the `linspace`: bin = logspace(0, 3, 4); % Log-spaced binning (1, 10, 100, 1000) – Ratbert Feb 06 '15 at 20:28
  • This is what I meant by 95% confidence interval. I want to make the error bars represent this. http://www.mathworks.com/help/stats/prob.toolboxfittableparametricdistribution.paramci.html#inputarg_pd I have been trying to incorporate this function instead of standard deviation, but have been unsuccessful. (P.S. just started to learn programming, so sorry if I come across as rigid) – Haren Shetty Feb 08 '15 at 14:04
  • Well, you just have to replace `e(i) = std(...);` by `e(i) = diff(paramci(...))/2;` with the parameters you want. That's quite unrelated to the initial question. – Ratbert Feb 08 '15 at 15:36
  • `e(i) = diff(paramci(...))/2`will not work cause `paramci` function requires a pdf as input. So I did this instead. Does this make sense. `m = NaN(n,1); s = NaN(n,1); e = NaN(n,1); for i = 1:n m(i) = mean(y(ind==i)); % Mean value over the bin s(i) = std(y(ind==i)); % Standard deviation N = sum(y(ind==i)); % number of responses se(i) = s(i)./sqrt(N); %standard error e(i) = 1.96*se(i); %95% confidence interval end` – Haren Shetty Feb 11 '15 at 22:10