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

ShowMedian/mean for both halves of violin plots?

Open frebergstrom opened this issue 3 years ago • 4 comments

Hi,

I was wondering if its possible to show median/mean for two different violin plot halves that are next to each other (for easy comparison)? Currently, unless I am doing something wrong, when I put 'ShowMedian' or 'ShowMean' to true, I only get the median and mean for the right violin plot half, not the left half. Is this something that already is possible to do, but I am doing it wrong, or something you plan to implement in the future?

Best Fredrik

frebergstrom avatar May 26 '22 18:05 frebergstrom

Hi @frebergstrom,

You are right the current implementation of side by side does not support showing means. However, you can do it, if you call the violins directly, skipping the violinplot function. See the code below.

For example,

n= 1; % i.e x position
hold on;
% First half (left)
vsl = Violin({MPG(strcmp(Origin, 'Japan'))},... % Here your data for the left plot
    n-0.05,... % 0.05 is half the separation between the violins 
    'HalfViolin','left',...
    'QuartileStyle','shadow',...
    'DataStyle', 'scatter',...
    'ShowNotches', false,...
    'ShowMean', true,...
    'ViolinColor', {[ 0.4940    0.1840    0.5560 ]});
% Now plot the other half
vsr = Violin({MPG(strcmp(Origin, 'Japan'))*1.15},... % Here your data for the right plot
    n+0.05,...
    'HalfViolin','right',...
    'QuartileStyle','shadow',...
    'DataStyle', 'scatter',...
    'ShowNotches', false,...
    'ShowMean', true,...
    'ViolinColor', {0.4940    0.1840    0.5560});

Best, Miguel

mikelgg93 avatar May 27 '22 07:05 mikelgg93

Thanks Miguel,

however, I am getting an error that 'HalfViolin' is not a recognized parameter, I did not see any other name that would do the same, and just taking it out gives me a two sided plot. So what am I missing to create half-sided plots when calling violins directly?

Best Fredrik

frebergstrom avatar May 27 '22 09:05 frebergstrom

@frebergstrom Do you have the last version of the code? Please perform a git status & git pull. If you are not familiar with git, you can just download the last version in zip from here

After that would you mind letting me know if it's solved? For me, it runs without a problem

mikelgg93 avatar May 27 '22 10:05 mikelgg93

Thanks Miguel!

Yes, that solved it!

frebergstrom avatar May 27 '22 10:05 frebergstrom