一维插值比较简单,给出几个x,然后对应的y,即可直接用scipy的interpolate进行一维插值操作,这里我们选择多个插值方法,针对自己的情况选择:
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
x = np.arange(0, 10)
y = np.exp(-x/3.0)
kinds = ['linear', 'nearest', 'nearest-up', 'zero', 'slinear', 'quadratic', 'cubic', 'previous', 'next']
plt.figure(figsize=(20, 12))
for kind in kinds:
f = interpolate.interp1d(x, y, kind = kind)
xnew = np.arange(0, 9, 0.1)
ynew = f(xnew)
plt.plot(x, y, 'o')
plt.plot(xnew, ynew, '-',linewidth=4.0, label=kind)
plt.xlabel('x')
plt.ylabel('y')
plt.legend(fontsize=30)
plt.show()
以上是内插法,也可以使用外插发,只需要加一个fill_value即可,但是注意,内插法要比外插法精确很多:
f = interpolate.interp1d(x, y, kind = kind, fill_value='extrapolate')