After doing groupby in a pandas data-frame I wanna set subplots into different plots stacked next to eachother however, the module puts all of them in one plot
df.groupby('week')['label'].plot(kind='density', legend=True)
After doing groupby in a pandas data-frame I wanna set subplots into different plots stacked next to eachother however, the module puts all of them in one plot
df.groupby('week')['label'].plot(kind='density', legend=True)
Consider looping through the groupby
object and plot to corresponding axes:
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()
I think you want to do
df.groupby('week')['label'].plot(kind='density', legend=True, subplots=True)