diff --git a/Violin.m b/Violin.m index e1c74ca..bd3c9a3 100644 --- a/Violin.m +++ b/Violin.m @@ -106,13 +106,13 @@ args = obj.checkInputs(data, pos, varargin{:}); data = data(not(isnan(data))); if numel(data) == 1 - obj.MedianPlot = scatter(pos, data, 'filled'); + obj.MedianPlot = scatter(args.Parent, pos, data, 'filled'); obj.MedianColor = args.MedianColor; obj.MedianPlot.MarkerEdgeColor = args.EdgeColor; return end - hold('on'); + hold(args.Parent, 'on'); % calculate kernel density estimation for the violin if isempty(data) @@ -139,19 +139,19 @@ end jitter = 2*(rand(size(data))-0.5); obj.ScatterPlot = ... - scatter(pos + jitter.*jitterstrength, data, 'filled'); + scatter(args.Parent, pos + jitter.*jitterstrength, data, 'filled'); % plot the violin obj.ViolinPlot = ... % plot color will be overwritten later fill([pos+density*width pos-density(end:-1:1)*width], ... - [value value(end:-1:1)], [1 1 1]); + [value value(end:-1:1)], [1 1 1], 'Parent', args.Parent); % plot the mini-boxplot within the violin quartiles = quantile(data, [0.25, 0.5, 0.75]); obj.BoxPlot = ... % plot color will be overwritten later fill(pos+[-1,1,1,-1]*args.BoxWidth, ... [quartiles(1) quartiles(1) quartiles(3) quartiles(3)], ... - [1 1 1]); + [1 1 1], 'Parent', args.Parent); % plot the data mean meanValue = mean(data); @@ -163,7 +163,7 @@ if meanDensityWidthidx if iscell(varargin{idx+1}) grouporder = varargin{idx+1}; @@ -65,6 +67,19 @@ error('Second argument of ''GroupOrder'' optional arg must be a cell of category names') end end + + % parse the optional axis handle + AxisHandle = gca; + idx = find(strcmp(varargin, 'Parent')); + if ~isempty(idx) && numel(varargin)>idx + if ishghandle(varargin{idx+1}) + AxisHandle = varargin{idx+1}; + else + error('Second argument of ''Parent'' optional arg must be a valid axis handle') + end + end + + % tabular data if isa(data, 'dataset') || isstruct(data) || istable(data) @@ -85,7 +100,7 @@ thisData = data.(catnames{n}); violins(n) = Violin(thisData, n, varargin{:}); end - set(gca, 'XTick', 1:length(catnames), 'XTickLabels', catnames); + set(AxisHandle, 'XTick', 1:length(catnames), 'XTickLabel', catnames); % 1D data, one category for each data point elseif hascategories && numel(data) == numel(cats) @@ -104,12 +119,12 @@ thisData = data(cats == thisCat); violins(n) = Violin(thisData, n, varargin{:}); end - set(gca, 'XTick', 1:length(catnames), 'XTickLabels', catnames_labels); + set(AxisHandle, 'XTick', 1:length(catnames), 'XTickLabel', catnames_labels); % 1D data, no categories elseif not(hascategories) && isvector(data) violins = Violin(data, 1, varargin{:}); - set(gca, 'XTick', 1); + set(AxisHandle, 'XTick', 1); % 2D data with or without categories elseif ismatrix(data) @@ -117,11 +132,15 @@ thisData = data(:, n); violins(n) = Violin(thisData, n, varargin{:}); end - set(gca, 'XTick', 1:size(data, 2)); + set(AxisHandle, 'XTick', 1:size(data, 2)); if hascategories && length(cats) == size(data, 2) - set(gca, 'XTickLabels', cats); + set(AxisHandle, 'XTickLabel', cats); end end + + if nargout > 0 + varargout{1} = violins; + end end