-1

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.

Pinguim
  • 3
  • 1
  • 3
  • 1
    `geom_violin` isn't going to work, you'll likely need to either fashion your own or some fancy work with `geom_density`. – r2evans Jul 22 '19 at 00:18
  • I added a example (picture) – Pinguim Jul 22 '19 at 00:29
  • Related, I think you'll find the next step(s) here: https://stackoverflow.com/q/9763578/3358272 – r2evans Jul 22 '19 at 00:31
  • I think it is not the same problem, I dont know if i was clear (english isnt my first language) – Pinguim Jul 22 '19 at 00:47
  • The biggest differences between my linked answer and your suggested graphic are: (1) swapped x and y axes; and (2) fill. Both of those have multiple questions already on SO, so are less critical at the moment. – r2evans Jul 22 '19 at 01:06

1 Answers1

3

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

Brendan
  • 3,901
  • 15
  • 23