I have a time series with daily data, that I want to plot, and only plot a x-tick every month. I have tried multiple approaches (including the ones described here, but it seems that pandas considers my data from 1970, even if the date index is from another date. Example below:
date_range = pd.date_range(start='2023-01-01', periods=periods)
data = {
'A': np.random.randint(1, 10, periods),
'B': np.random.randint(1, 10, periods),
}
df = pd.DataFrame(data, index=date_range)
df
df['total'] = df.sum(axis=1)
df['rolling'] = df['total'].rolling(3).mean()
ax = df[['A', 'B']].plot(kind='bar', stacked=True)
# this one defaults to 1970—which seems to be a known problem
# https://stackoverflow.com/questions/69101233/using-dateformatter-resets-starting-date-to-1970
# ax.xaxis.set_major_locator(MonthLocator())
# ax.xaxis.set_major_formatter(DateFormatter('%b %Y'))
# this one also doesn't work, because the data is thought to be also from 1970 so the picture comes out very wrong
monthly_ticks = pd.date_range(start=df.index.min(), end=df.index.max(), freq='ME')
ax.set_xticks(monthly_ticks)
ax.set_xticklabels(monthly_ticks, rotation=45);```