以日期格式格式化Matplotlib图表X标签



我正在尝试在panda中按组绘制对象,但我希望日期以"Month-YY"的格式显示。目前,x轴以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()

你知道怎么做吗?感谢

您可以使用panda。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'))

最新更新