简体   繁体   English

如何使用scipy.interpolate获得顺序点插值?

[英]How to use scipy.interpolate to get sequential point interpolation?

I used scipy.interpolate to draw interpolation curves between points 我使用scipy.interpolate在点之间绘制插值曲线

Here is the python code. 这是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()

and I get this figure 我得到这个数字 在此处输入图片说明

But I would like to get this one 但我想得到这个 在此处输入图片说明

How this can be achieved ? 如何实现呢?

You can use interpolate.splrep to interpolate a parametric curve. 您可以使用interpolate.splrep插入参数曲线。 As described in the Scipy reference page about Interpolation . 如Scipy参考页中有关插值所述 Add parameter k=1 to get a linear spline fit. 添加参数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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM