Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set positions of the plots #61

Open
noirchen opened this issue Jun 7, 2023 · 5 comments
Open

How to set positions of the plots #61

noirchen opened this issue Jun 7, 2023 · 5 comments

Comments

@noirchen
Copy link

noirchen commented Jun 7, 2023

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?

@NicolasClairis
Copy link
Contributor

NicolasClairis commented Mar 20, 2024

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

@noirchen
Copy link
Author

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

@NicolasClairis
Copy link
Contributor

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

@bastibe
Copy link
Owner

bastibe commented Mar 20, 2024

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!

@NicolasClairis
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants