Reputation: 11765
I have a time series of monthly data like this, and plot it like this:
rng = pd.date_range('1965-01-01', periods=600, freq='M')
ts = pd.Series(np.random.randn(len(rng)), index=rng)
fig, ax = plt.subplots()
ts.plot(ax=ax)
The major tick marks are set every 10 years, beginning in 1969. I'd like to change this so they start in 1975. After looking at some matplotlib samples (here and here) I tried adding
from matplotlib.dates import YearLocator, DateFormatter
decs = YearLocator(10) # decades
decsFmt = DateFormatter("%Y")
ax.xaxis.set_major_locator(decs)
ax.xaxis.set_major_formatter(decsFmt)
datemin = pd.datetime(ts.index.min().year, 1, 1)
datemax = pd.date(ts.index.max().year + 1, 1, 1)
ax.set_xlim(datemin, datemax)
but this doesn't work.
Upvotes: 1
Views: 197
Reputation: 429
If you want to use matplotlib to set axis limits you will need to turn off pandas' date formatting.
Just change the line to
ts.plot(x_compat=True, ax=ax)
and it should work.
Upvotes: 1