From 373065d06784446c068fd46dd1d92db5b395dcda Mon Sep 17 00:00:00 2001 From: Srinivas Gorur-Shandilya Date: Fri, 24 Apr 2020 13:44:54 -0400 Subject: [PATCH 1/2] fixed a bug where fragmented categorical data crashed the plot function --- Violin.m | 3 +++ violinplot.m | 19 +++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Violin.m b/Violin.m index 5940f7d..e1c74ca 100644 --- a/Violin.m +++ b/Violin.m @@ -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)); diff --git a/violinplot.m b/violinplot.m index c151312..a22ee19 100644 --- a/violinplot.m +++ b/violinplot.m @@ -85,28 +85,31 @@ 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,'XTickLabelRotation',45); % 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) @@ -114,9 +117,9 @@ 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 From a4b7e8e02a388bfab0063b64ec64de9ecabf0268 Mon Sep 17 00:00:00 2001 From: Srinivas Gorur-Shandilya Date: Fri, 24 Apr 2020 14:53:47 -0400 Subject: [PATCH 2/2] removed default rotation --- violinplot.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/violinplot.m b/violinplot.m index a22ee19..57264f8 100644 --- a/violinplot.m +++ b/violinplot.m @@ -104,7 +104,7 @@ thisData = data(cats == thisCat); violins(n) = Violin(thisData, n, varargin{:}); end - set(gca, 'XTick', 1:length(catnames), 'XTickLabels', catnames_labels,'XTickLabelRotation',45); + set(gca, 'XTick', 1:length(catnames), 'XTickLabels', catnames_labels); % 1D data, no categories elseif not(hascategories) && isvector(data)