I am trying to build my own chart with the datetime axis. I understand that matplotlib requires a floating format, which is days from the era. So, I want to convert a numpy array to the era of float, as matplotlib requires.
The datetime values are stored in a numpy array named t:
In [235]: t
Out[235]: array(['2008-12-01T00:00:59.000000000-0800',
'2008-12-01T00:00:59.000000000-0800',
'2008-12-01T00:00:59.000000000-0800',
'2008-12-01T00:09:26.000000000-0800',
'2008-12-01T00:09:41.000000000-0800'], dtype='datetime64[ns]')
Apparently matplotlib.dates.date2num only accepts a python datetimes sequence as input (not numpy datetimes arrays):
import matplotlib.dates as dates
plt_dates = dates.date2num(t)
raises AttributeError: object 'numpy.datetime64' does not have attribute 'toordinal'
How do I solve this problem? I hope to have a solution that works for all types of numpy.datetime as an object.
( ) - date2num . :
z = np.array([0]).astype(t.dtype)
plt_dates = (t - z)/ np.timedelta64(1,'D')
, .