Matplotlib中的熊猫datetimeindex错误地截断了

时间:2018-10-24 22:09:23

标签: python pandas matplotlib datetimeindex

我对以下代码感到困惑:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime, timedelta
from random import random
x=pd.date_range(start='1/1/2018', periods=20, freq='H')
# xpy=x.to_pydatetime()
# x+=timedelta(milliseconds=1)
y=[random() for i in range(len(x))]
# s = pd.Series(y, index=x)
s = pd.Series(y, index=x)
# format the ticks
s.plot(figsize=(10,3))
ax = plt.gca()
# ax.plot(x,y)
ax.xaxis.set_major_locator(mdates.MonthLocator())
# ax.xaxis.set_minor_locator(mdates.DayLocator())

monthFmt = mdates.DateFormatter('%b')
dayFmt = mdates.DateFormatter('%d')
ax.xaxis.set_major_formatter(monthFmt)  # This is what you needed
ax.xaxis.set_minor_formatter(dayFmt)  # This is what you needed
ax.tick_params(direction='out', pad=15)
# format the coords message box

应该在2018年1月1日在x轴上格式化几次;但是,该图显然是错误的,如下所示:

enter image description here

更有趣的是,如果将milliseconds=1 timedelta添加到原始索引,则x轴被证明是正确的:

enter image description here

有人可以解释为什么吗? 这是pandasmatplotlib中的错误吗?

环境信息如下: macOS 10.14,python 3.7,matplotlib 3.0.0,pandas 0.23.4

0 个答案:

没有答案