仅使用numpy与Interp1d插值时间序列
我想使用标记为确切点和插值来绘制使用Numpy和Matplotlib的时间序列。基本上这(数据是虚拟的,但是功能是相同的,请注意,时间点之间的距离可能会有所不同):
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
T = [
np.datetime64('2020-01-01T00:00:00.000000000'),
np.datetime64('2020-01-02T00:00:00.000000000'),
np.datetime64('2020-01-03T00:00:00.000000000'),
np.datetime64('2020-01-05T00:00:00.000000000'),
np.datetime64('2020-01-06T00:00:00.000000000'),
np.datetime64('2020-01-09T00:00:00.000000000'),
np.datetime64('2020-01-13T00:00:00.000000000'),
]
Z = [543, 234, 435, 765, 564, 235, 345]
fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot()
ax.plot(T, Z, 'o-')
但是,这里完成的插值只是连接了点。我想使用Scipy的Interp1d包括样条插值和其他类型。因此,我尝试用以下内容替换最后一行:
ax.plot(T,Z, 'o')
ax.plot(T,interp1d(T, Z)(T), '-')
并且会收到以下错误:
UFuncTypeError: ufunc 'true_divide' cannot use operands with types dtype('float64') and dtype('<m8[ns]')
阅读此答案,我读到,在插值期间,我应该划分t
by np.timedeltelta64(1,'s')
,像这样:
ax.plot(T,Z, 'o')
ax.plot(T,interp1d(T/np.timedelta64(1, 's'))(T), '-')
但是,我遇到了一个更奇怪的错误:
ufunc 'true_divide' cannot use operands with types dtype('<M8[ns]') and dtype('<m8[s]')
我该怎么办?
I want to plot a time series with numpy and matplotlib, using markers for the exact points, and interpolation. Basically this (data is dummy, but functionality is the same, note that distance between time-points may vary):
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
T = [
np.datetime64('2020-01-01T00:00:00.000000000'),
np.datetime64('2020-01-02T00:00:00.000000000'),
np.datetime64('2020-01-03T00:00:00.000000000'),
np.datetime64('2020-01-05T00:00:00.000000000'),
np.datetime64('2020-01-06T00:00:00.000000000'),
np.datetime64('2020-01-09T00:00:00.000000000'),
np.datetime64('2020-01-13T00:00:00.000000000'),
]
Z = [543, 234, 435, 765, 564, 235, 345]
fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot()
ax.plot(T, Z, 'o-')
However, the interpolation done here is just connecting the points. I want to include spline interpolation and other kinds using scipy's interp1d. So, I tried replacing the last line with the following:
ax.plot(T,Z, 'o')
ax.plot(T,interp1d(T, Z)(T), '-')
and I get the following error:
UFuncTypeError: ufunc 'true_divide' cannot use operands with types dtype('float64') and dtype('<m8[ns]')
Reading this answer, I read that during interpolation I should divide T
by np.timedelta64(1, 's')
, like this:
ax.plot(T,Z, 'o')
ax.plot(T,interp1d(T/np.timedelta64(1, 's'))(T), '-')
however, I get an even weirder error:
ufunc 'true_divide' cannot use operands with types dtype('<M8[ns]') and dtype('<m8[s]')
What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
t
中的任何元素的数据类型是np.datetime64
而不是np.timedelta64
。因此,通过使用datatype
/generated/scipy.interpaly.interp1d.html“ rel =“ nofollow noreferrer”>文档建议,我们必须通过
x
和y
是可转换的要像scipy.interpaly.interp1d
浮动值浮动值以获取插值功能。我们将使用此答案做到这一点:最后,我们可以使用以下方式使用以下方式来绘制 :
结合所有内容,我们将获得以下输出:
可以重现图像的代码:
The data type of any element in
T
isnp.datetime64
and notnp.timedelta64
.Thus, convert the dtype of all elements of T to
np.timedelta64
by creating a numpy array with datatypem
:Then, as the documentation suggests, we have to pass
x
andy
that are convertible to float like values toscipy.interpolate.interp1d
to get a interpolation function. We'll use a method suggested in this answer to do that:Finally, we can use the interpolated function as follows for plotting:
Combining everything, we get the following output:
The code that can reproduce the image: