Formatted xticklabels on pandas plot show as 1971

124 Views Asked by At

I've imported data from an Excel file and I plot it with Matplotlib. If I use only default settings I get accurate results with correct tick labels, even if not formatted the way I want.

df.plot()

default plot

But trying to use ConciseDateFormatter yields bizarre results

ax=df.plot()
locator = mdates.AutoDateLocator(minticks=5, maxticks=10)
formatter = mdates.ConciseDateFormatter(locator)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
plt.show()

odd output

ConciseDateFormatter makes some odd decisions here, including tagging the whole date series as 'October 1971'. The data actually spans from Feb 2021 through Sep 2023. Note that this is not the '1970' problem that others have asked about. 1970 and 1971 are different years.

The big question: why is it selecting and displaying this weird date, and how can I fix it?

The smaller question: how do I get it to display only month names and, where appropriate, years, replacing those random numbers on the x-axis in some locations?

0

There are 0 best solutions below