为什么在 matplotlib.plot_date 中每月的第一天自动绘制为刻度? [英] Why is the first of the month automatically plotted as tick in matplotlib.plot_date?

查看:61
本文介绍了为什么在 matplotlib.plot_date 中每月的第一天自动绘制为刻度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以日期为字符串和日期为datetime64 [ns]的地理数据框.我正在使用 matplotlib 3.0.2.我尝试使用 plt.cla() plt.clear() plt.clf()清除数据,如其他帖子和问题,但无论如何,数据在没有任何提示的情况下绘制了似乎是长轴和短轴格式的内容 - 假设在 plot_date 中,每月的第一天是长"轴.这是我的图表(为清晰起见进行了编辑).似乎是由于3.0.2(来自2.1.0)中的新更新引起的,很难弄清楚到底是什么引起了

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)

更新

感谢Jody和ImportanceOfBeingErnest指出这是由于matplotlib版本3.0.2中的kwarg更改所致,该更改已在3.1中修复,其中 interval_multiples = True 现在是默认绘制第一个除了其他刻度的月份.@ImportanceOfBeingErnest 是评论者.

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.

公关链接

最相关的 matplotlib 问题 #12925.Matplot库问题#9978 也很重要

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

推荐答案

感谢@TheImportanceOfBeingErnest和@JodyKlymak提出的意见,建议使用 AutoDateLocator MonthLocator .以下代码段最终成为我的解决方法.最终图

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

控制更多的方法:

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)

在保留旧行为的同时仍然使用 AutoDateLocator 的方法:

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

这篇关于为什么在 matplotlib.plot_date 中每月的第一天自动绘制为刻度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆