Plotting multiple boxplots in seaborn?
# libraries
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
from random import randint, gauss
# create a DataFrame
df = pd.DataFrame({
'Sensations':[randint(1,3) for i in range(300)]
})
df['Temperature'] = df['Sensations'].map(lambda x: gauss(0.8/x,0.1)*40)
df['Sensations'] = df['Sensations'].map({1:'hot',2:'normal',3:'cold'})
# create plot
ax = sns.boxplot(x="Sensations", y="Temperature", data=df)
# show plot
plt.show()
Consider first assigning a grouping column like Trial for each corresponding dataframe, then pd.concat
your dataframes, and finally pd.melt
the data for a indicator/value long-wise dataframe before plotting with seaborn. Below demonstrates with random data:
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
np.random.seed(44)
# DATAFRAMES WITH TRIAL COLUMN ASSIGNED
df1 = pd.DataFrame(np.random.randn(5,5), columns=list(range(1,6))).assign(Trial=1)
df2 = pd.DataFrame(np.random.randn(5,5), columns=list(range(1,6))).assign(Trial=2)
df3 = pd.DataFrame(np.random.randn(5,5), columns=list(range(1,6))).assign(Trial=3)
cdf = pd.concat([df1, df2, df3]) # CONCATENATE
mdf = pd.melt(cdf, id_vars=['Trial'], var_name=['Number']) # MELT
print(mdf.head())
# Trial Number value
# 0 1 1 -0.750615
# 1 1 1 -1.715070
# 2 1 1 -0.963404
# 3 1 1 0.360856
# 4 1 1 -1.190504
ax = sns.boxplot(x="Trial", y="value", hue="Number", data=mdf) # RUN PLOT
plt.show()
plt.clf()
plt.close()