Search code examples
pythonggplot2seabornviolin-plot

Split violin plot for two variables with seaborn


I would like to create a split violin plot for two variables only. There is a lack of examples like this on internet. Using => http://seaborn.pydata.org/generated/seaborn.violinplot.html

For example: VAR1: 2, 3, 5, 6, 2, 4, 5 and VAR2: 3, 2, 5, 6, 2, 4, 6

in this case, Y axis would be the values and X axis both data (variables), and "hue" would be both data as well.

I am having trouble creating this plot.

the only example I found was that, but has nothing to do with my data.


Solution

  • Using seaborn, you can get the basic plot by melting your dataframe, generating a false x-axis variable, and using the split option in sns.violinplot.

    import pandas as pd
    import seaborn as sns
    
    df = pd.DataFrame({'VAR1':[2, 3, 5, 6, 2, 4, 5],
                       'VAR2':[3, 2, 5, 6, 2, 4, 6]})
    
    df2 = df.melt().assign(x='vars')
    
    sns.violinplot(data=df2, x='x', y='value', 
                   hue='variable', split=True, inner='quart')
    

    enter image description here

    Adapted from https://seaborn.pydata.org/examples/grouped_violinplots.html