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.
I want to plot boxplots and violinplots comparing each element of each category. Something like this:
Would you help me?
CodePudding user response:
Seaborn expects long-form data where each row corresponds to one observation.
melt
the data byCategory
: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
Use this
melted
dataframe withboxplot
andviolinplot
:sns.boxplot(data=melted, x='Element', y='Concentration', hue='Category')
sns.violinplot(data=melted, x='Element', y='Concentration', hue='Category')