Matplotlib Plot Every Other Month w/Ticks and Grid lines Each Month

Viewed 308

I have code that looks like this below and I'm trying to plot every other month on the x axis starting with "Jan" with xticks/grid lines vertical at every or each month and year shown. However, i've tried two solutions and nothing happens and i get no error. In other words, it seems like the options I've tried don't have any effect on the plot. Here is the code for the plot portion and you can see I've tried "MonthLocator" and "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()

Here is my plot so far below. Than you for any help with this!

enter image description here

0 Answers
Related