Вот решение с использованием Seaborn's FacetGrid
, которое делает такие вещи действительно легкими
g = sns.FacetGrid(data=df_hb_SLR, col="Condition", hue='Game_RS', height=5, aspect=0.5)
g = g.map(sns.kdeplot, 'A_mean_per_subject', shade=True)
g.add_legend()
![enter image description here](https://i.stack.imgur.com/wbj87.png)
Недостаток FacetGrid
заключается в том, что он создает новую фигуру, поэтому если вы хотите интегрировать эти графики в больший ансамбль вспомогательных сюжетов, вы можете достичь того же результата, используя groupby()
и некоторые циклы:
group1 = "Condition"
N1 = len(df_hb_SLR[group1].unique())
group2 = 'Game_RS'
target = 'A_mean_per_subject'
height = 5
aspect = 0.5
colour = ['gray', 'blue', 'green', 'darkorange']
fig, axs = plt.subplots(1,N1, figsize=(N1*height*aspect,N1*height*aspect), sharey=True)
for (group1Name,df1),ax in zip(df_hb_SLR.groupby(group1),axs):
ax.set_title(group1Name)
for (group2Name,df2),c in zip(df1.groupby(group2), colour):
sns.kdeplot(df2[target], shade=True, label=group2Name, ax=ax, color = c)
![enter image description here](https://i.stack.imgur.com/y2JX9.png)