Skip to content

Commit

Permalink
Merge pull request #14 from HoldenLab/master
Browse files Browse the repository at this point in the history
Plot violins in a specified order
  • Loading branch information
bastibe authored Jul 17, 2019
2 parents ece71df + 53c76d0 commit 1a62a2b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
21 changes: 21 additions & 0 deletions test_cases/testviolinplot.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

% TEST CASE 1
display('Test 1: Violin plot default options');
load carbig MPG Origin
Origin = cellstr(Origin);
figure
vs = violinplot(MPG, Origin);
ylabel('Fuel Economy in MPG');
xlim([0.5, 7.5]);

display('Test 1 passed ok');

% TEST CASE 2
display('Test 2: Test the plot ordering option');
grouporder={'USA','Sweden','Japan','Italy','Germany','France','England'};

figure;
vs2 = violinplot(MPG,Origin,'GroupOrder',grouporder);
display('Test 2 passed ok');

%other test cases could be added here
23 changes: 22 additions & 1 deletion violinplot.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,27 @@
% Defaults to false
% 'ShowMean' Whether to show mean indicator
% Defaults to false
% 'GroupOrder' Cell of category names in order to be plotted.
% Defaults to alphabetical ordering

% Copyright (c) 2016, Bastian Bechtold
% This code is released under the terms of the BSD 3-clause license

hascategories = exist('cats','var') && not(isempty(cats));

%parse the optional grouporder argument
%if it exists parse the categories order
% but also delete it from the arguments passed to Violin
grouporder = {};
idx=find(strcmp(varargin, 'GroupOrder'));
if ~isempty(idx) && numel(varargin)>idx
if iscell(varargin{idx+1})
grouporder = varargin{idx+1};
varargin(idx:idx+1)=[];
else
error('Second argument of ''GroupOrder'' optional arg must be a cell of category names')
end
end

% tabular data
if isa(data, 'dataset') || isstruct(data) || istable(data)
Expand All @@ -73,7 +89,12 @@

% 1D data, one category for each data point
elseif hascategories && numel(data) == numel(cats)
cats = categorical(cats);
if isempty(grouporder)
cats = categorical(cats);
else
cats = categorical(cats, grouporder);
end

catnames = categories(cats);
for n=1:length(catnames)
thisCat = catnames{n};
Expand Down

0 comments on commit 1a62a2b

Please sign in to comment.