如何使用scipy.interpolate得到顺序点插值?
How to use scipy.interpolate to get sequential point 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()
我得到这个数字
但是我想要这个
如何实现?
您可以使用 interpolate.splrep to interpolate a parametric curve. As described in the Scipy reference page about 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()
我用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()
我得到这个数字
但是我想要这个
如何实现?
您可以使用 interpolate.splrep to interpolate a parametric curve. As described in the Scipy reference page about 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()