Skip to content

Commit

Permalink
Merge pull request #18 from sg-s/master
Browse files Browse the repository at this point in the history
fixed a bug where fragmented categorical data crashed the plot function
  • Loading branch information
bastibe committed Apr 27, 2020
2 parents 1a62a2b + a4b7e8e commit 7839fd0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Violin.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@
hold('on');

% calculate kernel density estimation for the violin
if isempty(data)
return
end
[density, value] = ksdensity(data, 'bandwidth', args.Bandwidth);
density = density(value >= min(data) & value <= max(data));
value = value(value >= min(data) & value <= max(data));
Expand Down
19 changes: 11 additions & 8 deletions violinplot.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,38 +85,41 @@
thisData = data.(catnames{n});
violins(n) = Violin(thisData, n, varargin{:});
end
set(gca, 'xtick', 1:length(catnames), 'xticklabels', catnames);
set(gca, 'XTick', 1:length(catnames), 'XTickLabels', catnames);

% 1D data, one category for each data point
elseif hascategories && numel(data) == numel(cats)

if isempty(grouporder)
cats = categorical(cats);
else
cats = categorical(cats, grouporder);
end

catnames = categories(cats);
for n=1:length(catnames)
thisCat = catnames{n};
catnames = (unique(cats)); % this ignores categories without any data
catnames_labels = {};
for n = 1:length(catnames)
thisCat = catnames(n);
catnames_labels{n} = char(thisCat);
thisData = data(cats == thisCat);
violins(n) = Violin(thisData, n, varargin{:});
end
set(gca, 'xtick', 1:length(catnames), 'xticklabels', catnames);
set(gca, 'XTick', 1:length(catnames), 'XTickLabels', catnames_labels);

% 1D data, no categories
elseif not(hascategories) && isvector(data)
violins = Violin(data, 1, varargin{:});
set(gca, 'xtick', 1);
set(gca, 'XTick', 1);

% 2D data with or without categories
elseif ismatrix(data)
for n=1:size(data, 2)
thisData = data(:, n);
violins(n) = Violin(thisData, n, varargin{:});
end
set(gca, 'xtick', 1:size(data, 2));
set(gca, 'XTick', 1:size(data, 2));
if hascategories && length(cats) == size(data, 2)
set(gca, 'xticklabels', cats);
set(gca, 'XTickLabels', cats);
end

end
Expand Down

0 comments on commit 7839fd0

Please sign in to comment.