如何使用scipy.interpolate进行连续点插值?

时间:2018-03-16 10:26:31

标签: python scipy interpolation

我使用scipy.interpolate在点之间绘制插值曲线

这是python代码。

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

x =np.array([1,2,3,4,3,2])
y = np.array([1,1,1,1,2,2])
f = interpolate.interp1d(x, y,kind='linear')
xnew = np.arange(1, 4, 0.01)
ynew = f(xnew)   # use interpolation function returned by `interp1d`
plt.plot(x, y, 'o', xnew, ynew, '-')
plt.show()

我得到这个数字 enter image description here

但我想得到这个 enter image description here

如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

您可以使用interpolate.splrep插入参数曲线。如Scipy参考页面中关于Interpolation所述。添加参数k = 1以获得线性样条拟合。

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

x =np.array([1,2,3,4,3,2])
y = np.array([1,1,1,1,2,2])
tck, u = interpolate.splprep([x, y], s=0., k=1)
unew = np.arange(0, 1.01, 0.01)
out = interpolate.splev(unew, tck)
plt.plot(x, y, 'o', out[0], out[1], '-')
plt.show()

enter image description here