【问题标题】:Matplotlib x-ticks goes from Jan-Jan instead of Jan-DecMatplotlib x-ticks 来自 Jan-Jan 而不是 Jan-Dec
【发布时间】:2020-01-27 22:22:15
【问题描述】:

我有一年中从 1 月 1 日到 12 月 31 日的每日天气数据。我正在绘制这些每日数据,并希望我的 x-ticks 是每月间隔。但是当我绘制它时,x-tick 来自 Jan-Jan 而不是 Jan-Dec。我如何摆脱最后一个具有 Jan 的 x-tick 并将其限制到 12 月。

如果我将 xlim 设置为从一月到十二月,则图表将覆盖整个轴。我想要从线条开始和结束的地方有一点差距。

日期格式为 YYYY-MM-DD

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime

plt.figure()
plt.plot(minmax2014['Date'], minmax2014['min'], color='r', label='Record High')
plt.plot(minmax2014['Date'], minmax2014['max'], color='b', label='Record Low')

plt.legend(loc=2)

d=minmax2014['Date'].values
plt.gca().fill_between(d, minmax2014['max'], minmax2014['min'], facecolor='yellow', alpha=0.3)

plt.gca().xaxis.set_major_locator(mdates.MonthLocator())
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b'))

【问题讨论】:

  • 是否100%确认又一年1月没有数据?这将改变答案
  • 这里的刻度在,例如Dec 01。您仍然需要显示Dec 02,..., Dec 31。事实上,Dec 31 与明年的 Jan 01 非常接近,所以有另一个刻度 Jan 是有意义的(想想 1.992 带有整数刻度)
  • 是的,确认没有另一年的1月份的数据。有没有办法在每月 15 号左右而不是每月 1 号左右更改几个月的 xtick?我宁愿没有新的 Jan xtick,因为数据是一年的。谢谢。

标签: python pandas date matplotlib


【解决方案1】:

我只是设置 xlimit 或刻度。您当然可以制作自己的定位器,但这对于您正在做的事情来说似乎有点过分了。

请注意,我在这里使用了datetime64,但您可以轻松地使用datetime

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

fig, ax = plt.subplots(3, 1, constrained_layout=True)
dates = np.arange('2000-01-01T01:00:00', '2000-12-31T23:00:00',
                  dtype='datetime64[h]')
y = np.random.randn(len(dates))
for a in ax:
    a.plot(dates, y)

ax[1].set_xlim(np.datetime64('1999-12-31T23:59:59'), np.datetime64('2000-12-31T23:00:00'))


ticks = [np.datetime64('2000-%02d-01'%month) for month in range(1, 13)]
ax[2].set_xticks(ticks)
ax[2].xaxis.set_major_formatter(mdates.DateFormatter('%b'))

for a in ax[:-1]:
    a.xaxis.set_major_locator(mdates.MonthLocator())
    a.xaxis.set_major_formatter(mdates.DateFormatter('%b'))
plt.show()

【讨论】:

  • 我试图避免在轴上开始和结束,但似乎这可能是唯一可行的方法。谢谢
  • 非常感谢。
【解决方案2】:

正如您在评论中提到的,是的,我们可以将刻度移到月中:

minmax2014 = pd.DataFrame({
    'min': np.random.uniform(0,10,365),
    'max': np.random.uniform(90,100,365),
    'Date':pd.date_range('2014-01-01', '2014-12-31', freq='D')
}, )
plt.figure()
plt.plot(minmax2014['Date'], minmax2014['min'], color='r', label='Record High')
plt.plot(minmax2014['Date'], minmax2014['max'], color='b', label='Record Low')

plt.legend(loc=2)

d=minmax2014['Date'].values
plt.gca().fill_between(d, minmax2014['max'], minmax2014['min'], facecolor='yellow', alpha=0.3)

# new ticks at 15th of months
ticks = pd.date_range('2014-01-01', '2014-12-31', freq='MS') + pd.Timedelta(days=14)

# set ticks
plt.xticks(ticks)

# set tick names
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b'))

输出:

【讨论】:

  • 真的 真的建议不要将刻度移到月中,除非您每个月只有一个数据点。没有理智的人会看到情节并说“一月”标记是 1 月 15 日。
  • 我也宁愿离开Jan。但是 OP 要求移动刻度:-)。
  • 感谢您的帮助。我现在明白为什么移动刻度线是个坏主意。但是谢谢你,学到了一些关于移动 x-ticks 的新知识。基于此,我们是否可以只显示 12 个刻度而不是 13 个刻度,这样最后一月就不会出现?(当刻度在每个月的第一天时)
猜你喜欢
  • 2023-02-06
  • 1970-01-01
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多