I have resampled data from a monthly series to plot the yearly mean value. However, the year ticks show up in the following format in the X axis:
However, I want only the years to be shown, not the entire timestamp. But the following code does not plot the graph correctly.
ax = umtmvsDF.resample('A').mean().plot.bar(title='Average value per year',grid=True,color=(.21,.42,.12),figsize=(12,10))
ax.set(xlabel=Constants.TIME,ylabel=Constants.USD)
ax.xaxis.set_major_formatter(dates.DateFormatter('%Y'))
plt.show()
This code shows all the ticks to be equal to 1970. What am I doing wrong here?
EDIT: SOLUTION:: @Rene shared the link which finally solved the question. It provides the reason 1970 showed up in the 2nd graph.
Revised code
tempDF3 = umtmvsDF.resample ( 'A' ).mean ()
ax = tempDF3.plot.bar ( title='Average value per year', grid=True, color=(.21, .42, .12),
figsize=(12, 10) )
ax.set ( xlabel=Constants.TIME, ylabel=Constants.USD )
ticklabels = ['']*len(tempDF3.index)
ticklabels[::5] = [item.strftime('%Y') for item in tempDF3.index[::5]]
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))
plt.gcf().autofmt_xdate()
plt.show ()