bastibe / Violinplot-Matlab

Violin Plots for Matlab
https://mathworks.com/matlabcentral/fileexchange/170126-violinplot-matlab
BSD 3-Clause "New" or "Revised" License
253 stars 103 forks source link

Two half violinplot #49

Open jon6295 opened 2 years ago

jon6295 commented 2 years ago

Hi

A short question. Would it be possible to

Would it be possible to tell us how you have managed to do the Italy vs Italy 1.5 of the second example. When I try to produce a violin plot which have different distributions in each half I can not make it so that they are separated.

Thanks!

mikelgg93 commented 2 years ago

Ofc! I just called directly to the violin function, skipping the violinplot. Here goes an example of the code:

n= 4; % The original position, in this case 4 was Italy

vsl = Violin({MPG(strcmp(Origin, grouporder{n}))},...         %Your data
    n-0.05,...                                                                          % 0.05 is the offset
    'HalfViolin','left',...                                                           % just left side violin plot
    'QuartileStyle','shadow',...
    'DataStyle', 'scatter',...
    'ShowNotches', false,...
    'ShowMean', true,...
    'ShowMedian', true);                                               

% Same for the right violin
vsr = Violin({MPG(strcmp(Origin, grouporder{n}))*1.15},...
    n+0.05,... % Note here that I add the offset
    'HalfViolin','right',...
    'QuartileStyle','shadow',...
    'DataStyle', 'scatter',...
    'ShowNotches', false,...
    'ShowMean', true);
GrimmSnark commented 2 years ago

Hello,

When I try to replicate the above example, I get the following error:

Index in position 1 is invalid. Array indices must be positive integers or logical values.

Error in Violin (line 375)
                    ViolinColor{1} = args.ViolinColor{1}(pos,:);

Am I missing something?

Best,

Michael

vk-vivek-kumar commented 2 years ago

Michael

Hi Michael,

I was having the same issue. I fixed it. Replace the following 2 lines in Violin.m code:

Original line in code: ViolinColor{1} = args.ViolinColor{1}(pos,:); Replace this line with: ViolinColor{1} = args.ViolinColor{1}(round(pos),:)

Also, right below it is another instance that you need to replace. Original line in code: ViolinColor{2} = args.ViolinColor{2}(pos,:) Replace this line with: ViolinColor{2} = args.ViolinColor{2}(round(pos),:)

These are around Line# 373 in the code. I hope this helps!

Thanks, Vivek

GrimmSnark commented 2 years ago

Thank you so much for the suggestion!

EitNickS commented 1 year ago

@mikelgg93, are you able to explain how one can get the text instead of values, as is shown in the original picture?

Screen Shot 2022-12-12 at 3 53 42 PM

Using your example code, you get the position that you are explicitly putting down as n (which is 4). Using Violin (not violinplot), I cannot figure out how to use category labels as names for the individual violins and then allow for an offset. Violin's code explicitly requires scalar number inputs for each violin's 'ID'.

Screen Shot 2022-12-12 at 3 53 20 PM
mikelgg93 commented 1 year ago

Hi! I might have used violinplot if I recall it properly due to its flexibility. Unfortunately, I no longer have access to Matlab to test it.

For the labels, you can define xticks and xticklabels in Matlab posthoc