Pandas 如何使我的x标签以月为单位递增?

Pandas 如何使我的x标签以月为单位递增?,pandas,dataframe,datetime,matplotlib,seaborn,Pandas,Dataframe,Datetime,Matplotlib,Seaborn,我有一个数据框: 我想创建一个图表,其中的xlabel与记录所在的月份相对应。我试过: df['date'] = pd.to_datetime(df['date']) df.set_index('date') l = sns.lineplot(x=df.index,y='Appliances',data=df, sort=False, color='grey') plt.xticks(rotation=45) # Set the locator locator = mdates.MonthLoc

我有一个数据框:

我想创建一个图表,其中的xlabel与记录所在的月份相对应。我试过:

df['date'] = pd.to_datetime(df['date'])
df.set_index('date')
l = sns.lineplot(x=df.index,y='Appliances',data=df, sort=False, color='grey')
plt.xticks(rotation=45)
# Set the locator
locator = mdates.MonthLocator()  # every month
# Specify the format - %b gives us Jan, Feb...
fmt = mdates.DateFormatter('%b')
X = plt.gca().xaxis
X.set_major_locator(locator)
X.set_major_formatter(fmt)
plt.show()
这给了我:

如您所见,XLabel没有显示。。我不知道为什么。
谢谢

感谢您的评论,我发现问题在于我没有将新数据框设置为具有更改的索引:

df = df.set_index('date')

如果您只使用10行数据帧,会发生什么情况?A将允许您找出这不起作用的原因。另请参见。
df.set_index('date')
这将在不更改
df
的情况下返回新的数据帧。数据帧仍按整数索引,因此日期格式化程序无效。将其更改为
df=df.set_index('date')
df.set_index('date',inplace=True)