我正在尝试在 Pandas 中绘制我的 groupby 对象,但我希望以“Month - YY”格式显示日期。目前,x 轴以完整的 date.time 格式显示日期,即 YYYY-MM-DD。
当前代码:
groupby_object.plot.bar(stacked = False)
plt.rcParams['figure.figsize'] = (19,8)
plt.legend(title = 'Line of Business', loc = 'centre left', bbox_to_anchor = (1,0.5)
plt.show()
知道如何解决这个问题吗?谢谢
答案 0 :(得分:2)
您可以使用 pandas.DatetimeIndex.strftime() 将日期时间索引转换为所需的格式。
axes = groupby_object.plot.bar(stacked = False)
# Ensure your index is datetime type
groupby_object.index = pd.to_datetime(groupby_object.index)
axes.set_xticklabels(df.index.strftime('%m-%Y'))