如何在Python中使用matplotlib更改轴的X轴?

如何在Python中使用matplotlib更改轴的X轴?,python,matplotlib,Python,Matplotlib,我正在尝试将轴的X轴从“2017-01”更改为“2017-12” 但是,我做不到 这是我使用的数据集 df_daily.groupby(["Date"])[["Track Name"]].head(1) 索引的日期时间和列的日期是相同的值 日期值介于“2017-01-01”和“2017-12-31”之间 这里是一个代码和结果 df_for_extracting_song = df_daily[(df_daily["Artist"] == "Ed Sheeran") & (df_dai

我正在尝试将轴的X轴从“2017-01”更改为“2017-12”

但是,我做不到

这是我使用的数据集

df_daily.groupby(["Date"])[["Track Name"]].head(1)

索引的日期时间和列的日期是相同的值

日期值介于“2017-01-01”和“2017-12-31”之间

这里是一个代码和结果

df_for_extracting_song = df_daily[(df_daily["Artist"] == "Ed Sheeran") & (df_daily["Region"] == "global") & (df_daily["Position"] <= 100) & (df_daily["Date"] <= "2017-12-31")]
df_ed_sheeran = df_for_extracting_song.groupby(['Track Name']).size()
df_ed_sheeran = df_ed_sheeran.reset_index()
df_ed_sheeran = df_ed_sheeran[df_ed_sheeran[0] >= 20]
df_ed_sheeran = df_ed_sheeran.set_index("Track Name")
df_ed_sheeran = df_for_extracting_song[df_for_extracting_song['Track Name'].isin(df_ed_sheeran.index)]
df_ed_sheeran = df_ed_sheeran.reset_index()
plt.figure(figsize=(15,8))
ax = sns.scatterplot(x="Date", y="Position", data=df_ed_sheeran, hue="Track Name")
ax.invert_yaxis()
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0, fontsize=18)

df_-for_-extracting_-song=df_-daily[(df_-daily[“Artist”]=“Ed Sheeran”)&(df_-daily[“Region”]=“global”)&(df_-daily[“Position”]您需要为xaxis使用定位器和格式化程序

import matplotlib.dates as md

months = md.MonthLocator()
month_fmt = md.DateFormatter('%Y-%m')
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(month_fmt)
X轴上重叠标签的常见解决方案是同时旋转标签

plt.xticks(rotation=45, ha='right')
ha表示水平对齐,在这种情况下,勾号将位于标签的右侧


编辑:我应该提到,只有当您的索引类型为datetime时,这才有效。很难判断您的datetime索引是否包含问题中使用的某些措辞。

非常感谢您的帮助

我试过以下两种方法

一,

将matplotlib.dates作为md导入

非常感谢你这么早告诉我!我会试试!谢谢你告诉我。我试过你说的,但没有成功。我把帖子放在现在的位置。请告诉我你是否能帮我。你叫什么“2”在您的回答中,我看起来是正确的。我无法测试它为什么不起作用,因为我无法运行代码。谢谢。我感谢您所做的。您正在查看的数据的最大和最小日期时间是多少?看起来您正在尝试绘制从2001年到2017年的数据。如果您在那里使用月定位器,它仍将尝试绘制216个月的数据(x个记号)并且仍然会导致重叠。您应该将索引限制在您感兴趣的时间范围内。您的“日期”是字符串。您希望将它们转换为实际日期,例如,通过
df[“Date”]=pd.to_datetime(df[“Date”])
,那么轴应该自动标记。谢谢你告诉我。我尝试了你说的,但没有成功。我在现在的位置贴了一个帖子。请告诉我你是否能帮我。
import matplotlib.dates as md

df_for_extracting_song = df_daily[(df_daily["Artist"] == "Ed Sheeran") & (df_daily["Region"] == "global") & (df_daily["Position"] <= 100) & (df_daily["Date"] <= "2017-12-31")]
df_ed_sheeran = df_for_extracting_song.groupby(['Track Name']).size()
df_ed_sheeran = df_ed_sheeran.reset_index()
df_ed_sheeran = df_ed_sheeran[df_ed_sheeran[0] >= 20]
df_ed_sheeran = df_ed_sheeran.set_index("Track Name")
df_ed_sheeran = df_for_extracting_song[df_for_extracting_song['Track Name'].isin(df_ed_sheeran.index)]
df_ed_sheeran = df_ed_sheeran.reset_index()

plt.figure(figsize=(15,8))
ax = sns.scatterplot(x="Datetime", y="Position", data=df_ed_sheeran, hue="Track Name")
months = md.MonthLocator()
month_fmt = md.DateFormatter('%Y-%m')
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(month_fmt)
ax.invert_yaxis()
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0, fontsize=18)
import matplotlib.dates as md

df_for_extracting_song = df_daily[(df_daily["Artist"] == "Ed Sheeran") & (df_daily["Region"] == "global") & (df_daily["Position"] <= 100) & (df_daily["Date"] <= "2017-12-31")]
df_ed_sheeran = df_for_extracting_song.groupby(['Track Name']).size()
df_ed_sheeran = df_ed_sheeran.reset_index()
df_ed_sheeran = df_ed_sheeran[df_ed_sheeran[0] >= 20]
df_ed_sheeran = df_ed_sheeran.set_index("Track Name")
df_ed_sheeran = df_for_extracting_song[df_for_extracting_song['Track Name'].isin(df_ed_sheeran.index)]
df_ed_sheeran = df_ed_sheeran.reset_index()

plt.figure(figsize=(15,8))
ax = sns.scatterplot(x="Datetime", y="Position", data=df_ed_sheeran, hue="Track Name")
ax.invert_yaxis()
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0, fontsize=18)