I have a dataframe where the index is the first date of each month and the size
column is the frequency for that month, e.g.
Using .index
on the dataframe confirms the type of the index is DatetimeIndex
:
DatetimeIndex(['2006-12-01', ...],
dtype='datetime64[ns]', name='created_at_month', length=175, freq=None)
Using .plot()
on the DataFrame I can produce a line graph per month:
However, it only lists every other year on the x axis, and I'd like it to list each year on the axis.
I would expect to be able to do
ax.xaxis.set_major_locator(mdates.YearLocator(1))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
However this doesn't output any labels at all.
If I add a minor formatter (ax.xaxis.set_minor_formatter(mdates.DateFormatter('%d %m %Y'))
), I get this:
What am I doing wrong here to cause the dates to change?
The relevant versions are:
- Matplotlib: 3.3.4
- Pandas: 1.2.4
- Python: 3.8.8
CodePudding user response: