我试图根据它们的每月总活动时间来绘制三个数据框,所以我使用了groupby函数。
df110 = df[df['Attendance Abs Type Code'] == '110']
df120 = df[df['Attendance Abs Type Code'] == '120']
dfWithout110 = df[df['Attendance Abs Type Code'] != '110 ']
dfWithout110120 = dfWithout110[dfWithout110['Attendance Abs Type Code'] != '120']
dfWithout110120Chart = dfWithout110120[["Activity Hours"]].groupby(dfWithout110120["Activity Month"]).sum().plot(kind='bar', width=0.8, title="Total activity hours per month (Without 110 & 120)")
df110chart = df110[["Activity Hours"]].groupby(df110["Activity Month"]).sum().plot(kind='bar', width=0.8, title="Total activity hours per month (110 only)")
df120chart = df120[["Activity Hours"]].groupby(df120["Activity Month"]).sum().plot(kind='bar', title="Total activity hours per month (120 only)")
数据集https://drive.google.com/file/d/1YdSsP8BM4PVNh8m2kW7244NOo75lNUT7/view?usp=sharing的下载链接
以下是数据框中的一些示例数据:
请看所附图片。
我真正想要的实际上是像这样的堆叠式条形图:
答案 0 :(得分:1)
您可以在hue
中使用seaborn
参数:
import seaborn as sns
df['hue'] = df["Attendance Abs Type Code"]
df.loc[~df['hue'].isin(['110', '120']), 'hue'] = 'Other'
df = df.groupby(['Activity Month', 'hue'])['Activity Hours'].sum().reset_index()
sns.barplot(x='Activity Month', y='Activity Hours', hue='hue', data=df)