Violinplot-Matlab
Violinplot-Matlab copied to clipboard
How to set positions of the plots
In boxplot
there is a parameter positions
to put the boxes at specified locations. For example
data=[randn(50, 1); randn(60, 1) + 1; randn(70, 1) + 2; randn(80, 1) + 3; randn(90, 1) + 4]
group=[ones(50, 1); ones(60,1)+1; ones(70,1)+2; ones(80,1)+3; ones(90,1)+4]
boxplot(data, group, 'positions', [1, 3, 4, 6, 7])
How to do similar thing using violin plot?
I think you cannot do it with violinplot, but you can do it with Violin which is the function called by violinplot. At least, that's how I managed to get rid of that problem.
In your example above, I guess I would do something like that:
figure;
hold on; % will allow to display multiple plots in the same figure
index = [1, 3, 4, 6, 7];
% loop over your groups
for iGroup = 1:numel(unique(group))
Violin({data(group==iGroup)},index(iGroup));
end
I get the following output :)
This is good, but is it possible to use non-integer positions?
True I had the same problem because the variable "pos" is used multiple times in the script and has to be an integer because of the interdependencies so I guess that the easy answer is no, but you can always change your labels using xticks and xticklabels afterwards (at least that's how I avoided dealing with that) but maybe the author of the function @bastibe can propose a better solution
I suppose the problem is that pos
is used to index into ViolinColor
in https://github.com/bastibe/Violinplot-Matlab/blob/master/Violin.m#L379. That's to give each Violin its own color, according to the global color rotation. If you have a better solution for this, I'd be grateful for a pull request!
No idea on my side, but as I said using xticks and xticklabels Matlab functions allows to change the x.label afterwards. Another option would be to display the half violins to have them on the same tick.