Skip to content

Instantly share code, notes, and snippets.

@williamrowell
Created September 1, 2015 14:45
Show Gist options
  • Save williamrowell/e6d67298f0b983cd06d2 to your computer and use it in GitHub Desktop.
Save williamrowell/e6d67298f0b983cd06d2 to your computer and use it in GitHub Desktop.
scatter plots with error bars and jitter in MATLAB
function error_jitter(xvals, yvals, errvals)
%%ERROR_JITTER(xvals, yvals)
%
% xvals -> array of x positions
% yvals -> cell array of multiple y positions for each x position
% errvals -> cell array of multiple std/sem for each x position
% TODO: allow yvals/errvals to be ragged array
%
% author: William Rowell (rowellw@janelia.hhmi.org)
hold on
for xidx = 1:length(xvals)
% spread data over region from x-0.2 to x+0.2
num_yvals = length(yvals{xidx});
jitter_delta = 0.4 / num_yvals;
x = repmat(xvals(xidx),[num_yvals, 1]);
jitter = ((0:(num_yvals-1))*jitter_delta - 0.2);
x = x + jitter';
y = yvals{xidx};
err = errvals{xidx};
errorbar(x,y,err,'bo');
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment