Python matplotlib.dates.date2num:将numpy数组转换为matplotlib日期时间 [英] Python matplotlib.dates.date2num: converting numpy array to matplotlib datetimes

查看:904
本文介绍了Python matplotlib.dates.date2num:将numpy数组转换为matplotlib日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制带有日期时间轴的自定义图表.我的理解是matplotlib需要一个浮点格式,距时代已经过去了几天.因此,我想根据matplotlib的要求将一个numpy数组转换为float时期.

I am trying to plot a custom chart with datetime axis. My understanding is that matplotlib requires a float format which is days since epoch. So, I want to convert a numpy array to the float epoch as required by matplotlib.

datetime值存储在一个名为t的numpy数组中:

The datetime values are stored in a numpy array called 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]')

显然,matplotlib.dates.date2num仅接受一系列python日期时间作为输入(而不是numpy日期时间数组):

Apparently, matplotlib.dates.date2num only accepts a sequence of python datetimes as input (not numpy datetimes arrays):

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

引发AttributeError:'numpy.datetime64'对象没有属性'toordinal'

raises AttributeError: 'numpy.datetime64' object has no attribute 'toordinal'

我应如何解决此问题?我希望有一个适用于所有类型的numpy.datetime(如object)的解决方案.

How should I resolve this issue? I hope to have a solution that works for all types of numpy.datetime like object.

我最好的解决方法(我不确定是正确的)根本不使用date2num.相反,我尝试使用以下内容:

My best workaround (which I am not sure to be correct) is not to use date2num at all. Instead, I try to use the following:

z = np.array([0]).astype(t.dtype)
plt_dates = (t - z)/ np.timedelta64(1,'D')

即使此解决方案正确,也最好使用库函数,而不是手动的临时解决方法.

Even, if this solution is correct, it is nicer to use library functions, instead of manual adhoc workarounds.

推荐答案

要快速修复,请使用:

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数组... 就我而言,检查源代码后,问题似乎在于最新的matplotlib.cbook无法从numpy数组创建可迭代对象,并认为该数组是数字.

It seems the latest (matplotlib.__version__ '2.1.0') does not like numpy arrays... In my case, after checking the source code, the problem seems to be that the latest matplotlib.cbook cannot create an iterable from the numpy array and thinks the array is a number.

对于类似但更复杂的问题,请检查 http://stackoverflow.com /questions/13703720/converting-between-datetime-timestamp-and-datetime64 ,可能是,也许是(如果有人回答) 有人回答了,使用to_pydatetime()的代码似乎最好,而且:

For similar but a bit more complex problems, check http://stackoverflow.com/questions/13703720/converting-between-datetime-timestamp-and-datetime64, possibly Why do I get "python int too large to convert to C long" errors when I use matplotlib's DateFormatter to format dates on the x axis?, and maybe matplotlib plot_date AttributeError: 'numpy.datetime64' object has no attribute 'toordinal' (if someone answers) someone answered, his code using to_pydatetime() seems best, also: pandas 0.21.0 Timestamp compatibility issue with matplotlib, though that did not work in my case (because of python 2???)

这篇关于Python matplotlib.dates.date2num:将numpy数组转换为matplotlib日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆