[英]plotting a grouped bar chart with bins
我有一个类似于这样的 dataframe:
Create Date count1 count2 count3 count4
2018-01 12 21 12 123
2018-02 11 25 12 145
2018-08 12 26 12 145
2019-03 13 28 12 334
2019-06 15 22 12 345
2019-07 16 25 12 165
2020-01 12 25 12 178
2020-02 12 23 12 178
2020-03 12 26 12 187
2021-01 11 28 12 146
2021-02 12 29 12 123
2021-03 11 22 12 189
2022-01 17 21 12 167
2022-02 18 23 12 166
2022-03 19 23 12 123
我想要一个 x 轴是一年的分组条形图。 例如 2018 年、2019 年、2020 年、2021 年、2022 年。在每一年中,我都希望该年的某一列的总和为 1 个柱,依此类推。
我试过的
df_combined.plot.bar(logy=True)
plt.xticks(rotation=0)
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.show()
在调用plot
之前,您需要使用groupBy.sum
制作pandas.DatetimeIndex
:
(
df_combined
.assign(year= pd.DatetimeIndex(df_combined["Create Date"]).year)
.groupby("year").sum(numeric_only=True) #as_index=True by default
.plot(kind="bar", logy=True)
)
plt.xticks(rotation=0)
plt.legend(bbox_to_anchor=(1.05, 1), loc="upper left")
plt.show();
Output ( plot ):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.