Python matplotlib.dates.date2num: convert numpy array to matplotlib datetimes

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')

, .

+4
1

:

import matplotlib.dates as dates
plt_dates = dates.date2num(t.to_pydatetime())

import matplotlib.dates as dates
plt_dates = dates.date2num(list(t))

, (matplotlib.__ version__ '2.1.0') numpy... Edit: , , , matplotlib.cbook iterable numpy , .

, , fooobar.com/questions/39072/..., "python int , C long" , DateFormatter matplotlib x? , , matplotlib plot_date AttributeError: "numpy.datetime64" "toordinal" ( - ) : - , , to_pydatetime(), , : pandas 0.21.0 matplotlib, (- python 2???)

+6

Source: https://habr.com/ru/post/1624692/


All Articles