我在 Pandas 中有一个时间序列,其中的日期是在月底:
import pandas as pd
s = pd.Series({
'2018-04-30': 0,
'2018-05-31': 1,
'2018-06-30': 0,
'2018-07-31': 1,
'2018-08-31': 0,
'2018-09-30': 1,
})
s.index = pd.to_datetime(s.index)
当我用 matplotlib 绘制它时,我得到了我期望的结果,点在月末,线开始于 2018 年 5 月之前:
import matplotlib.pyplot as plt
plt.plot(s)
但是 Pandas 的原生绘图函数绘制了月初的点:
s.plot()
我以为这可能只是 Pandas 将“4 月 30 日”标记为“4 月”,但事实似乎并非如此:
s2 = pd.Series([0.2, 0.7], index=pd.date_range('2018-05-01', '2018-05-02'))
s.plot()
s2.plot()
这是 Pandas 中的错误还是我在这里做错了什么?
最佳答案
这似乎是 pandas 中的一个错误。要获得与 matplotlib 相同的效果,您可以始终使用 x_compat=True
。这也将允许使用 matplotlib.dates
格式化程序和定位器。
s = pandas.Series(...)
s.plot(x_compat=True)
关于python - Pandas 日期在月初被错误地绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49816776/