如何在matplotlib中绘制datetime.time? [英] How plot datetime.time in matplotlib?

查看:94
本文介绍了如何在matplotlib中绘制datetime.time?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示两个数组:

x : [datetime.time(0, 17, 47, 782000), ...ecty : [1712, 2002, ...等

我正在尝试将x转换为matplotlib所使用的格式,但是它永远不会醒来

x = [matplotlib.dates.date2num(i) for i in x]

但我收到此错误

  AttributeError:'datetime.time'对象没有属性'toordinal'

我的问题与时间格式有关:原始信息是这样的:

  00:04:49.251

然后我将其转换

  datetime.datetime.strptime(string,%H:%M:%S.%f").time()

所以类型是

解决方案

这里的问题是您指定了 datetime.time 对象,这些对象只包含一天中的时间信息.正如 Emanuele Paolini 在他们的评论中指出的那样,matplotlib 希望有 datetime.datetime 对象除了时间之外还带有日期.>

您可以执行转换:

导入日期时间my_day = datetime.date(2014, 7, 15)x_dt = [x中t的datetime.datetime.combine(my_day,t)]

现在,您可以将 x_dt plot 函数一起使用.

这种方法的缺点是您可能会在绘图的某处获取日期,但这可以通过更改标签设置来解决.

I have two arrays I want to display :

x : [datetime.time(0, 17, 47, 782000), ...ect
y : [1712, 2002, ...ect

I'm trying to convert x to the format used by matplotlib but it never woks

x = [matplotlib.dates.date2num(i) for i in x]

But I get this error

AttributeError: 'datetime.time' object has no attribute 'toordinal'

My problem is linked with the time format : The raw info is like this :

00:04:49.251

Then I convert it

datetime.datetime.strptime(string, "%H:%M:%S.%f").time()

So the type is

<type 'datetime.time'>

解决方案

The problem here is that you specify datetime.time objects which only have the information on the time of day. As Emanuele Paolini points out in their comment, matplotlib expects to have datetime.datetime objects which carry the date in addition to the time.

You may carry out the conversion:

import datetime

my_day = datetime.date(2014, 7, 15)
x_dt = [ datetime.datetime.combine(my_day, t) for t in x ]

Now you may use x_dt with the plot function.

The downside of this approach is that you may get the date somewhere on your plot, but that can be tackled by changing the label settings.

这篇关于如何在matplotlib中绘制datetime.time?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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