1

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 mentioned in other posts and questions but no matter what, the data plots what seems to be a major and minor axis formatting without any prompting - assuming that in plot_date the first of the month is the "major" axis. This is my graph (edited for clarity). It seems caused by new updates in 3.0.2 (from 2.1.0) it's hard to figure out what exactly is causing this

I saw this question/answer which seems to be in the vein of my issue, but in this instance it was desired to be using sub dates, etc, while I am confused since this has not been a consistent issue for me in other similar datasets.

Dataframe:
          Date    var_name
106 2018-04-03  0.79132056
216 2018-04-09  0.75112718
546 2018-06-12  0.73359674
646 2018-07-03  0.72600174
706 2018-07-23  0.71263647



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)

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

UPDATE

Thanks to Jody and ImportanceOfBeingErnest for pointing out this was caused by a change in kwargs in matplotlib version 3.0.2 that is fixed in 3.1 where interval_multiples=True is now the default to plot the first of the month in addition to other ticks. @ImportanceOfBeingErnest was a reviewer.

PR link

most relevant matplotlib issue #12925. Matplot lib issue #9978 is also relevant

Elizabeth
  • 31
  • 7
  • I don't think those are minor ticks. Could you rotate the labels, such that they become readable? – ImportanceOfBeingErnest Jan 04 '19 at 00:59
  • @ImportanceOfBeingErnest sure thing- for some reason `fig.autofmt_xdate()` did not have an effect of rotating the labels which is why this method was used – Elizabeth Jan 04 '19 at 17:25
  • That is definitely a problem with the `AutoDateLocator` in use. It ticks the 29th of each month in addition. I fear the only way to circumvent this is to use a `MonthLocator`. You can open [an issue](https://github.com/matplotlib/matplotlib/issues) about it if you want. – ImportanceOfBeingErnest Jan 04 '19 at 17:31
  • I wouldn't say it was a bug, but a change so that AutoDateLocator that set the kwarg `interval_multiple=True`. That kwarg tries to make the first of the month a tick no matter what. If you set it to False, you should largely get the old behaviour back (you'll have to manually set the locator though). – Jody Klymak Jan 10 '19 at 06:05

1 Answers1

0

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)
Elizabeth
  • 31
  • 7