Home > Net >  Boxplot and violin plot with multiple categories
Boxplot and violin plot with multiple categories

Time:11-09

I have a chemical database. It contains the chemical analysis of different elements in 4 different categories.

I made a simplified version of what my df looks like, in reality, it has more elements.

enter image description here

I want to plot boxplots and violinplots comparing each element of each category. Something like this:

enter image description here

Would you help me?

CodePudding user response:

Seaborn expects long-form data where each row corresponds to one observation.

  1. melt the data by Category:

    melted = df.melt('Category', var_name='Element', value_name='Concentration')
    
    #    Category  Element  Concentration
    # 0         A       Au         0.1000
    # 1         A       Au         0.1500
    # 2         A       Au         0.4000
    # 3         B       Au         0.1000
    # ...
    # 10        D       Au         1.0000
    # 11        A       Ag         1.0000
    # ...
    # 20        D       Ag        40.0000
    # 21        D       Ag        56.0000
    
  2. Use this melted dataframe with boxplot and violinplot:

    sns.boxplot(data=melted, x='Element', y='Concentration', hue='Category')
    
    sns.violinplot(data=melted, x='Element', y='Concentration', hue='Category')
    
  • Related