I'm writing a script that plots some data with dates on the x axis (in matplotlib). I need to create a numpy.linspace
out of those dates in order to create a spline afterwards. Is it possible to do that?
What I've tried:
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)
It throws this error:
TypeError: unsupported operand type(s) for *: 'datetime.datetime' and 'float'
I've also tried converting datetime
to np.datetime64
, but that doesn't work as well:
dates = [np.datetime64(i) for i in dates]
x = np.linspace(min(dates), max(dates), 500)
Error:
TypeError: ufunc multiply cannot use operands with types dtype('<M8[us]') and dtype('float64')
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…