在 groupby 之后,将子图设置为彼此相邻的图,而不是在一个图中

after groupby, set subplots into plots next to each-other rather than in one plot

在 pandas 数据帧中进行 groupby 之后,我想将子图设置为彼此相邻堆叠的不同图,但是,该模块将它们全部放在一个图中

df.groupby('week')['label'].plot(kind='density', legend=True)

我觉得你想做

df.groupby('week')['label'].plot(kind='density', legend=True, subplots=True)

考虑遍历 groupby 对象并绘制到相应的轴:

import matplotlib.pyplot as plt
...

week_grps = df.groupby('week')
fig, axs = plt.subplots(nrows=1, ncols=len(week_grps), figsize=(15,5))

for ax,(i, sub) in zip(axs, week_grps):
    sub['label'].plot(kind='density', legend=True, title=i, ax=ax)

plt.tight_layout()
plt.show()
plt.clf()
plt.close()