首页 > 解决方案 > 如何将 Xaxis(次)的格式更改为其他格式?Python - 熊猫 - Matplotlib

问题描述

我尝试创建条形图,但 X 轴格式有问题。在图中,我有年月日 00:00:00。但我只想要年月,所以我尝试了这段代码:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.dates import DateFormatter

ax = df_graph.plot(kind='bar', figsize=(12, 8), legend=False) 

plt.title('Groups Unavailability by Month')
plt.ylabel('Unavailability (hours)')
plt.xlabel('Date (Year-Month)')

hfmt = mdates.DateFormatter('%Y-%m')
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(hfmt)

plt.xticks(rotation=0)

三行 hftm、major_locator、major_formatter 没有效果……

谢谢你的时间 !

标签: pythonpandasmatplotlib

解决方案


推荐阅读