我使用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()
如何实现这一目标?
答案 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()