我讀到的一些原因是由於實現此功能的困難和時間以某種方式保持穩定。該提案已過時(至少自2012年起),您可以看到here。
已經提出了幾種解決方法,您可以在SO中找到像this和this這樣的示例。
至於matplotlib lib本身,您甚至會發現一些解決方案似乎可以作爲最初的原型。這篇文章發表於agijsberts在這個討論中here。儘管問題出在pandas追蹤器中,但實際上解決方案似乎完全不需要熊貓。
from matplotlib import units, dates
from matplotlib import pyplot as plt
from numpy import datetime64, timedelta64, arange, ndarray, dtype
from numpy.random import rand
import datetime
resolution_scale = {
dtype('datetime64[ns]'): 1e-9,
dtype('datetime64[us]'): 1e-6,
dtype('datetime64[ms]'): 1e-3,
dtype('datetime64[s]'): 1,
dtype('datetime64[m]'): 60,
dtype('datetime64[h]'): 60 * 60,
dtype('datetime64[D]'): 24 * 60 * 60,
}
class Datetime64Converter(dates.DateConverter):
@staticmethod
def convert(values, unit, axis):
if isinstance(values, ndarray) and issubclass(values.dtype.type, datetime64):
return dates.epoch2num(values.view('i8') * resolution_scale[values.dtype])
elif isinstance(values, datetime.date):
return dates.date2num(values)
else:
return values
units.registry[datetime64] = Datetime64Converter
a = arange('2014-01-01', '2014-01-07', timedelta64(1, 'D'), dtype='datetime64[D]')
b = rand(len(a))
for i, r in enumerate(('ns', 'us', 'ms', 's', 'm', 'h', 'D')):
plt.plot(a.astype('datetime64[{0}]'.format(r)), b + i, label=r)
plt.legend()
plt.show()
然而,即使有這樣的,我還沒有聽說過一個實際的妥協要做到這一點(雖然注意到,我沒有保持自己或者通知)。就我個人而言,我會說這是因爲它不在任何優先列表。
非常感謝@armatita真的很好你花時間。你證實了我的疑慮,但想確認。再次感謝 – theakson