Reputation: 1
I want to plot like this in where x axes has As, Cr, Cd, Pb columns values and boxplots are color coded with fish column. Is it possible? my data(csv):
fish,As,Cr,Cd,Pb
T. ilisha,0.023,0.002,0.039,0.004
G. chapra,0.224,0.011,0.048,0.005
M. vittatus,0.678,0.015,0.236,0.106
G. giuris,0.368,0.011,0.179,0.037
C. punctatus,0.274,0.016,0.124,0.035
M. armatus,0.461,0.015,0.476,0.039
P. ticto,0.437,0.021,0.533,0.048
S. cascasia,0.301,0.009,0.068,0.011
A. mola,0.454,0.016,0.179,0.065
H. fossilis,0.423,0.023,0.423,0.117
L.bata,0.295,0.019,0.287,0.039
W. attu,0.448,0.019,0.231,0.035
Upvotes: 0
Views: 121
Reputation: 80289
Here is an approach to create boxplots per element, with a spot to indicate each fish.
pd.melt
is used to create a "long form" of the dataframe, which is easier for seaborn to work with. Basically 2 new columns are created, one with the element name and one with the corresponding value. Each original row gets converted into 4 new rows.
from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns
from io import StringIO
df_data = StringIO('''fish,As,Cr,Cd,Pb
T. ilisha,0.023,0.002,0.039,0.004
G. chapra,0.224,0.011,0.048,0.005
M. vittatus,0.678,0.015,0.236,0.106
G. giuris,0.368,0.011,0.179,0.037
C. punctatus,0.274,0.016,0.124,0.035
M. armatus,0.461,0.015,0.476,0.039
P. ticto,0.437,0.021,0.533,0.048
S. cascasia,0.301,0.009,0.068,0.011
A. mola,0.454,0.016,0.179,0.065
H. fossilis,0.423,0.023,0.423,0.117
L.bata,0.295,0.019,0.287,0.039
W. attu,0.448,0.019,0.231,0.035''')
df = pd.read_csv(df_data)
df_long = pd.melt(df, 'fish', var_name='element', value_name='value')
sns.boxplot(x='element', y='value', palette=['lightgrey'], data=df_long, showfliers=False)
sns.scatterplot(x='element', y='value', hue='fish', palette='Set3', edgecolor='black', marker='D', data=df_long, zorder=3)
plt.show()
PS: To avoid the overlapping markers of the scatterplot
, a stripplot
could be used instead:
sns.stripplot(x='element', y='value', hue='fish', palette='Set3', linewidth=1, edgecolor='black', marker='D', data=df_long, zorder=3)
For the scatterplot
(but not for the stripplot
), you could use different markers for each fish:
markers = ['o', 'v', '^', '8', '*', 'P', 'D', 'X', 's', 'p', '<', '>']
sns.scatterplot(x='element', y='value', hue='fish', palette='Set3', linewidth=1, edgecolor='black', markers=markers, data=df_long, zorder=3)
Upvotes: 1