scipy.interpolate.spline没有按预期工作

时间:2016-11-02 05:12:11

标签: scipy interpolation spline

我对这个有点难过。我已经使用样条线来成功地平滑我的数据,但这次它不起作用。以下是无效的代码片段。任何指针都将受到高度赞赏。

In [46]: x

Out[46]:
array([  600.,   650.,   700.,   750.,   800.,   850.,   900.,   950.,
        1000.,  1050.,  1100.,  1150.,  1200.,  1250.])

In [47]: y

Out[47]:
array([ 2.68530481,  3.715443  ,  4.11270841,  2.91720571,  1.49194971,
        0.24770035, -0.64713611, -1.40938122, -2.24634466, -3.04577225,
       -3.73914759, -4.35097303, -4.94702689, -5.56523414])

In [48]: x2=numpy.linspace(x.min(),x.max(),20)

In [49]: spline(x,y,x2)

Out[49]:
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.])

1 个答案:

答案 0 :(得分:0)

enter image description here

尝试使用interp1d代替spline which is deprecated (*)

import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate import interp1d

plt.ion()
x = np.array([600., 650., 700., 750., 800., 850., 900., 950.,
              1000., 1050., 1100., 1150., 1200., 1250.])
y = np.array([2.68530481, 3.715443, 4.11270841, 2.91720571, 1.49194971,
              0.24770035, -0.64713611, -1.40938122, -2.24634466,
              -3.04577225, -3.73914759, -4.35097303, -4.94702689,
              -5.56523414])
plt.plot(x,y)
x2 = np.linspace(x.min(), x.max(), 20)
f = interp1d(x, y, kind='cubic')
y2 = f(x2)
plt.plot(x2,y2)

输出

In [20]: x2
Out[20]:
array([  600.        ,   634.21052632,   668.42105263,   702.63157895,
         736.84210526,   771.05263158,   805.26315789,   839.47368421,
         873.68421053,   907.89473684,   942.10526316,   976.31578947,
        1010.52631579,  1044.73684211,  1078.94736842,  1113.15789474,
        1147.36842105,  1181.57894737,  1215.78947368,  1250.        ])

In [21]: y2
Out[21]:
array([ 2.68530481,  3.35699957,  4.03277746,  4.08420565,  3.31233485,
        2.29896296,  1.34965136,  0.48288214, -0.21322503, -0.76839036,
       -1.28566315, -1.84433723, -2.42194321, -2.96633554, -3.45993064,
       -3.90553288, -4.31968149, -4.7262301 , -5.13883472, -5.56523414])

(*)在其他工具下,scipy列表spline为:

  

为向后兼容而存在的函数(不应在新代码中使用):