Violinplot-Matlab icon indicating copy to clipboard operation
Violinplot-Matlab copied to clipboard

How to set positions of the plots

Open noirchen opened this issue 1 year ago • 5 comments

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?

noirchen avatar Jun 07 '23 14:06 noirchen

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 :) image

NicolasClairis avatar Mar 20 '24 12:03 NicolasClairis

This is good, but is it possible to use non-integer positions?

noirchen avatar Mar 20 '24 12:03 noirchen

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

NicolasClairis avatar Mar 20 '24 12:03 NicolasClairis

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!

bastibe avatar Mar 20 '24 15:03 bastibe

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.

NicolasClairis avatar Mar 20 '24 15:03 NicolasClairis