Reputation: 191
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?
Upvotes: 0
Views: 481
Reputation: 41327
Seaborn expects long-form data where each row corresponds to one observation.
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
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')
Upvotes: 1