Skip to content Skip to sidebar Skip to footer

Why Is The First Of The Month Automatically Plotted As Tick In Matplotlib.plot_date?

I have a geodataframe with date as string and Date as a datetime64[ns]. I am using matplotlib 3.0.2. I've tried clearing the data using plt.cla(), plt.clear() and plt.clf() as ment

Solution 1:

Thank you to @TheImportanceOfBeingErnest and @JodyKlymak for their comments to suggest using the AutoDateLocator and MonthLocator. The following snippet ended up being my workaround. Final figure

Approach with more control:

figsize=(14,10)
fig, ax = plt.subplots(figsize=figsize)
data['Date'] = pd.to_datetime(data['Date'])

plt.xticks(rotation=30, ha='right')
plt.grid(color='k', lw=0.2)

months = matplotlib.dates.MonthLocator()
ax.xaxis.set_major_locator(months)
year_month = matplotlib.dates.DateFormatter('%Y-%m')
ax.xaxis.set_major_formatter(year_month)

plt.plot_date(data.Date, data.var_name)

Approach still using AutoDateLocator while retaining old behavior:

locator = matplotlib.dates.AutoDateLocator(interval_multiples=False)
ax.xaxis.set_major_locator(locator)

Post a Comment for "Why Is The First Of The Month Automatically Plotted As Tick In Matplotlib.plot_date?"