在进行一些分析时,我偶然发现了一个 ValueError,我可以将其归结为以下简单示例,该示例可以重现我得到的错误:
import numpy as np
import matplotlib.pyplot as plt
import datetime as dt
x = np.array([dt.datetime(2012, 10, 19, 10, 0, 0),
dt.datetime(2012, 10, 19, 10, 0, 1),
dt.datetime(2012, 10, 19, 10, 0, 2),
dt.datetime(2012, 10, 19, 10, 0, 3)])
y = np.array([1, 3, 4, 2])
在尝试绘制这个简单的 x 和 y 数组时,我没有遇到以下问题:
fig, ax = plt.subplots()
ax.plot(x, y)
或者
fig, (ax1, ax2) = plt.subplots(nrows=2)
ax1.plot(x, y)
但是在添加时sharex=True
,我得到一个错误:
fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
ax1.plot(x, y)
错误信息:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 2445, in home
self._update_view()
File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 2818, in _update_view
self.draw()
File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 2796, in draw
loc.refresh()
File "C:\Python27\lib\site-packages\matplotlib\dates.py", line 758, in refresh
dmin, dmax = self.viewlim_to_dt()
File "C:\Python27\lib\site-packages\matplotlib\dates.py", line 530, in viewlim_to_dt
return num2date(vmin, self.tz), num2date(vmax, self.tz)
File "C:\Python27\lib\site-packages\matplotlib\dates.py", line 289, in num2date
if not cbook.iterable(x): return _from_ordinalf(x, tz)
File "C:\Python27\lib\site-packages\matplotlib\dates.py", line 203, in _from_ordinalf
dt = datetime.datetime.fromordinal(ix)
ValueError: ordinal must be >= 1
我从 matplotlib ( https://github.com/matplotlib/matplotlib/issues/162 ) 中发现了一个关于使用twinx
with dates 给出相同错误的问题。是同一个bug吗?这似乎是一个长期已知的错误,但尚未解决。