khemedi
khemedi

Reputation: 806

How to generate separate violinplots for each class and group

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.

Upvotes: 2

Views: 1701

Answers (2)

Trenton McKinney
Trenton McKinney

Reputation: 62413

Import and DataFrame

import pandas as pd
import seaborn as sns
import numpy as np  # for random data

# sample data
np.random.seed(2024)
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      0     A      8
1      0     A     36
2      0     A     65
3      1     A     66
4      0     A     74

Plotting

seaborn.violinplot

ax = sns.violinplot(data=dfm, x='Group', y='value', hue='Class')
sns.move_legend(ax, bbox_to_anchor=(1, 0.5), loc='center left', frameon=False)
  • 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'
  • Use hue='Group' or hue='Class' for color, however, each 'Class' and 'Group' is already uniquely identified, so adding color is redundant. Color should only be added if it conveys additional information.
g = sns.catplot(kind='violin', data=dfm, x='Class', y='value', col='Group', col_wrap=2)

enter image description here

Upvotes: 3

nonin
nonin

Reputation: 724

Using seaborn, it is pretty straightforward:

import seaborn as sns
...
sns.violinplot(x=df.Label, y=df.B)

Upvotes: 1

Related Questions