Matplotlib每隔一个月绘制一次,每个月带有刻度线和网格线

Matplotlib每隔一个月绘制一次,每个月带有刻度线和网格线,matplotlib,plot,format,axis-labels,Matplotlib,Plot,Format,Axis Labels,我的代码如下所示,我正试图每隔一个月在x轴上绘制一次,从“Jan”开始,在显示的每个月和年份处垂直绘制xticks/网格线。然而,我尝试了两种解决方案,但什么都没有发生,也没有出现错误。换句话说,我尝试过的选项似乎对情节没有任何影响。这是绘图部分的代码,您可以看到我尝试了“MonthLocator”和“mdates” 下面是我的情节。谢谢你的帮助 #PRODUCE AND VISUALIZE FORECAST pred_uc = results.get_forecast(steps=6) pr

我的代码如下所示,我正试图每隔一个月在x轴上绘制一次,从“Jan”开始,在显示的每个月和年份处垂直绘制xticks/网格线。然而,我尝试了两种解决方案,但什么都没有发生,也没有出现错误。换句话说,我尝试过的选项似乎对情节没有任何影响。这是绘图部分的代码,您可以看到我尝试了“MonthLocator”和“mdates”

下面是我的情节。谢谢你的帮助

#PRODUCE AND VISUALIZE FORECAST
pred_uc = results.get_forecast(steps=6)
pred_ci = pred_uc.conf_int()
import matplotlib.dates as mdates
#from matplotlib.dates import MonthLocator
ax = y['2019':].plot(label='observed', figsize=(14, 7))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
#ax.xaxis.set_major_locator(MonthLocator(interval=2))
pred_uc.predicted_mean.plot(ax=ax, label='Forecast')
ax.fill_between(pred_ci.index,
                pred_ci.iloc[:, 0],
                pred_ci.iloc[:, 1], color='k', alpha=.25)
ax.set_xlabel('Date')
ax.set_ylabel('MOS Wind Speed')
#add the LT monthly average to plot
from datetime import date
cm = date.today().month
lty = y.groupby(y.index.month).mean()
lty = lty.to_frame() 
lty.columns=['LT Mean']
ltyc = lty.iloc[cm-1:12].reset_index() # extract curr month to end of LT mean monthly wind speed
#create date sequence using date format of df = y
ltyc['Date'] = pd.to_datetime(ltyc["Date"], format='%m').apply(lambda dt: dt.replace(year=2020))#convert the "Date" col to yyyy-mm-dd 
ltycs = pd.Series(ltyc['LT Mean'].values, index=ltyc['Date'])#convert to Series since the other plots are in series format
ltycs.plot(label='LT Mean',ax=ax,color='k')
plt.grid(b=True, which='major', color='k', linestyle='-')
plt.grid(b=True, which='minor', color='g', linestyle='-', alpha=0.2)#alpha is the minor grid thickness w/higher numbers thicker lines
plt.minorticks_on()

plt.legend()
plt.show()