更改轴子图的 X 刻度

Change X ticks for axes subplot

我正在尝试设置轴子图的 x 刻度,但它对我不起作用。 现在我有以下数据框:

Data Frame

我正在使用以下代码绘制此图:

ax=predictions.plot(y=['Actual','add_add','add_mul','mul_add','mul_mul','Philips'],figsize=(5,4), 
                 legend=True,color=['b','r','g','m','y','black'],ylabel='Quantity',xlabel='Date')
ax

结果如下:

Plot

如您所见,8 月写为 2021 年 8 月。我希望能够将所有 x 刻度更改为仅月份或年份月份(例如,保留 7 月或设置 2021 年 7 月)

我一直在尝试使用 ax.set_xticks ,但到目前为止它对我不起作用。

提前致谢!

不要将 predictions.plot() 输出保存到 ax。您只会得到一个行列表。实施下面的解决方案后,您可以使用 plt.show().

查看输出

处理 datetime 对象的日期时

对于带有 'Jul 2021'、'Aug 2021' 等的标签,使用

import datetime
import matplotlib.pyplot as plt # Matplotlib.pyplot object

# Please note that df is a DataFrame object. Substitute your DataFrame object's name below. 
dates_index = df.index 

# You can use df.index in the list comprehension as well as in the plt.plot and plt.xticks operations as an alternative to dates_index.
labels = [f'{date.strftime("%b")} {date.strftime("%Y")}' for date in dates_index]
print(labels)

plt.plot(y=['Actual','add_add','add_mul','mul_add','mul_mul','Philips'],figsize=(5,4), 
                 legend=True,color=['b','r','g','m','y','black'],ylabel='Quantity',xlabel='Date')
plt.xticks(dates_index, labels)
plt.show() # Visualize the plot

对于带有 'Jul 21'、'Aug 21' 等的标签,使用

import datetime
import matplotlib.pyplot as plt # Matplotlib.pyplot object

# Please note that df is a DataFrame object. Substitute your DataFrame object's name below. 
dates_index = df.index

# You can use df.index in the list comprehension as well as in the plt.plot and plt.xticks operations as an alternative to dates_index.
labels = [f'{date.strftime("%b")} {date.strftime("%y")}' for date in dates_index]
print(labels)

plt.plot(y=['Actual','add_add','add_mul','mul_add','mul_mul','Philips'],figsize=(5,4), 
                 legend=True,color=['b','r','g','m','y','black'],ylabel='Quantity',xlabel='Date')
plt.xticks(dates_index, labels)
plt.show() # Visualize the plot

对于带有 'Jul'、'Aug' 等的标签,使用

import datetime
import matplotlib.pyplot as plt # Matplotlib.pyplot object

# Please note that df is a DataFrame object. Substitute your DataFrame object's name below.
dates_index = df.index 

# You can use df.index in the list comprehension as well as in the plt.plot and plt.xticks operations as an alternative to dates_index.
labels = [f'{date.strftime("%b")}' for date in dates_index]
print(labels)

plt.plot(y=['Actual','add_add','add_mul','mul_add','mul_mul','Philips'],figsize=(5,4), 
                 legend=True,color=['b','r','g','m','y','black'],ylabel='Quantity',xlabel='Date')
plt.xticks(dates_index, labels)
plt.show() # Visualize the plot

处理 str 个对象的日期时

对于带有 'Jul 2021'、'Aug 2021' 等的标签,使用

import datetime
import matplotlib.pyplot as plt # Matplotlib.pyplot object

# Please note that df is a DataFrame object. Substitute your DataFrame object's name below.
dates_index = df.index

# You can use df.index in the list comprehension as well as in the plt.plot and plt.xticks operations as an alternative to dates_index.
labels = [f'{(datetime.datetime.strptime(date, "%Y-%m-%d")).strftime("%b")} {datetime.datetime.strptime(date, "%Y-%m-%d").year}' for date in dates_index]
print(labels)

plt.plot(y=['Actual','add_add','add_mul','mul_add','mul_mul','Philips'],figsize=(5,4), 
                 legend=True,color=['b','r','g','m','y','black'],ylabel='Quantity',xlabel='Date')
plt.xticks(dates_index, labels)
plt.show() # Visualize the plot

对于带有 'Jul 21'、'Aug 21' 等的标签,使用

import datetime
import matplotlib.pyplot as plt # Matplotlib.pyplot object

# Please note that df is a DataFrame object. Substitute your DataFrame object's name below.
dates_index = df.index 

# You can use df.index in the list comprehension as well as in the plt.plot and plt.xticks operations as an alternative to dates_index.
labels = [f'{(datetime.datetime.strptime(date, "%Y-%m-%d")).strftime("%b")} {datetime.datetime.strptime(date, "%Y-%m-%d").strftime("%y")}' for date in dates_index]
print(labels)

plt.plot(y=['Actual','add_add','add_mul','mul_add','mul_mul','Philips'],figsize=(5,4), 
                 legend=True,color=['b','r','g','m','y','black'],ylabel='Quantity',xlabel='Date')
plt.xticks(dates_index, labels)
plt.show() # Visualize the plot

对于带有 'Jul'、'Aug' 等的标签,使用

import datetime
import matplotlib.pyplot as plt # Matplotlib.pyplot object

# Please note that df is a DataFrame object. Substitute your DataFrame object's name below.
dates_index = df.index 

# You can use df.index in the list comprehension as well as in the plt.plot and plt.xticks operations as an alternative to dates_index.
labels = [f'{(datetime.datetime.strptime(date, "%Y-%m-%d")).strftime("%b")}' for date in dates_index]
print(labels)

plt.plot(y=['Actual','add_add','add_mul','mul_add','mul_mul','Philips'],figsize=(5,4), 
                 legend=True,color=['b','r','g','m','y','black'],ylabel='Quantity',xlabel='Date')
plt.xticks(dates_index, labels)
plt.show() # Visualize the plot