2

I have a pandas dataframe:

df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
df['Label'] = np.random.randint(0,2,size=100)

I would like to create a figure in python where x-axis shows the class labels ('Class 0' and 'Class 1') and for each class and for a pre-defined variable like 'B' the violin plot (with a box-plot inside) is created.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
khemedi
  • 774
  • 3
  • 9
  • 19

2 Answers2

3

Import and DataFrame

import pandas as pd
import seaborn as sns

# sample data
df = pd.DataFrame(np.random.randint(0, 100, size=(100, 4)), columns=list('ABCD'))
df['Class'] = np.random.randint(0, 2, size=100)

# melt the dataframe to a long form
dfm = df.melt(id_vars='Class', var_name='Group')

# display(dfm.head())
   Class Group  value
0      1     A     12
1      1     A     43
2      0     A     58
3      1     A     49
4      1     A     47

Plotting

seaborn.violinplot

p = sns.violinplot(data=dfm, x='Group', y='value', hue='Class')
p.legend(title='Class', bbox_to_anchor=(1, 1), loc='upper left')
  • With x='Group', hue='Class'

enter image description here

  • With x='Class', hue='Group'

enter image description here

seaborn.catplot

  • To easily plot separately for each group, use seaborn.catplot with kind='violin'
sns.catplot(kind='violin', data=dfm, x='Class', y='value', col='Group', col_wrap=2)

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
1

Using seaborn, it is pretty straightforward:

import seaborn as sns
...
sns.violinplot(x=df.Label, y=df.B)
nonin
  • 704
  • 4
  • 10