【发布时间】:2016-10-24 03:55:18
【问题描述】:
我正在编写一个脚本,用 x 轴上的日期绘制一些数据(在 matplotlib 中)。我需要在这些日期之外创建一个numpy.linspace,以便之后创建样条曲线。有可能吗?
我尝试过的:
import datetime
import numpy as np
dates = [
datetime.datetime(2015, 7, 2, 0, 31, 41),
datetime.datetime(2015, 7, 2, 1, 35),
datetime.datetime(2015, 7, 2, 2, 37, 9),
datetime.datetime(2015, 7, 2, 3, 59, 16),
datetime.datetime(2015, 7, 2, 5, 2, 23)
]
x = np.linspace(min(dates), max(dates), 500)
它会抛出这个错误:
TypeError: unsupported operand type(s) for *: 'datetime.datetime' and 'float'
我也尝试将datetime 转换为np.datetime64,但效果不佳:
dates = [np.datetime64(i) for i in dates]
x = np.linspace(min(dates), max(dates), 500)
错误:
TypeError: ufunc multiply cannot use operands with types dtype('<M8[us]') and dtype('float64')
【问题讨论】:
-
numpy的datetime、np.datetime64包装器(我认为)可能会起作用。 -
已经试过了,有问题
标签: python datetime numpy matplotlib