简体   繁体   中英

How to use scipy.interpolate to get sequential point interpolation?

I used scipy.interpolate to draw interpolation curves between points

Here is the python code.

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. As described in the Scipy reference page about Interpolation . Add parameter k=1 to get a linear spline fit.

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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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