Skip to content

Commit

Permalink
add outputs and some options (e.g. 'xdata')
Browse files Browse the repository at this point in the history
  • Loading branch information
raltodo committed Apr 11, 2023
1 parent 9d6bca8 commit 4b33b2a
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions plotting/halfViolin.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,53 @@
% data should be arranged in columns; plot half violin for each

% default values
nColumns = size(data,2);
nBins = 1000;
smooth = 10;
maxWidth = 0.5;
colors = {'b','k','g','r','y','m','c'};
colors = get(gca,'ColorOrder'); if size(colors,1)<nColumns, colors = repmat(colors,nColumns,1); end
xdata = 1:nColumns;

for i = 1:2:length(varargin),
if ~ischar(varargin{i}),
error(['Parameter ' num2str(i+2) ' is not a property (type ''help <a href="matlab:help anovabox">anovabox</a>'' for details).']);
error(['Parameter ' num2str(i+2) ' is not a property (type ''help <a href="matlab:help halfViolin">halfViolin</a>'' for details).']);
end
switch(lower(varargin{i})),
case 'nbins',
nBins = varargin{i+1};
case 'colors'
colors = varargin{i+1};
case 'x'
xdata = varargin{i+1};
case 'smooth'
smooth = varargin{i+1};
smooth = varargin{i+1};
otherwise,
error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help anovabox">anovabox</a>'' for details).']);
error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help halfViolin">halfViolin</a>'' for details).']);
end
end
% make sure "colors" is a cell
if isa(colors,'double'), colormatrix = colors; colors = cell(size(colormatrix,1),1); for i=1:size(colormatrix,1), colors{i} = colormatrix(i,:); end; end
if ~isa(colors,'cell'), colors = repmat({colors},nColumns); end
if ~exist('smooth','var'),smooth = nBins/100; end % needs to be defined after "nBins" is provided

% generate probability distribution
limits = quantile(data(:),[0 1]) + [-1 1]*range(data(:));
[h,ht] = Dist(linspace(limits(1),limits(2),nBins),data);
h = Smooth(h,[smooth 0]);
if nColumns>1,h = Smooth(h,[smooth 0]); else, h = Smooth(h,smooth); end
h = h./max(h(:))*maxWidth;

clear handles
for i=1:size(h,2)
for i=1:nColumns
hh = h(:,i);
handles(i) = patch( i-1+[hh',zeros(1,numel(ht),1),0],[ht,fliplr(ht),ht(1)],colors{i} ); hold on
handles(i) = patch(xdata(i)+[hh',zeros(1,numel(ht),1),0],[ht,fliplr(ht),ht(1)],colors{i} ); hold on
set(handles(i),'EdgeColor','none','FaceAlpha',0.5);
a = prctile(data(:,i),2.5); b = prctile(data(:,i),97.5);
plot([i-1,i-1],[a,b],'k','LineWidth',3.0)
scatter(i-1,nanmean(data(:,i)),800,'k.')
plot(ones(1,2)*xdata(i),[a,b],'k','LineWidth',3.0)
scatter(xdata(i),nanmean(data(:,i)),800,'k.')
end

if nargout>0,
varargout{1} = handles;
varargout{2} = h;
varargout{3} = ht;
end

end

0 comments on commit 4b33b2a

Please sign in to comment.