import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import interpolate
#读数据
df=pd.read_excel("D:/Data/data_grundfox_r.xlsx" )
#绘制原始数据曲线
plt.figure(figsize=(10,5), dpi=80)
plt.subplot(121)
plt.plot(df.iloc[:,0],df.iloc[:,1],"ro")
#插值比例尺
#x = np.linspace(df.iloc[0,0],df.iloc[109,0],110)
xnew=np.linspace(df.iloc[0,0],df.iloc[109,0],220)
plt.subplot(122)
#插值方式,"nearest","zero"为阶梯插值,slinear 线性插值
for kind in ["nearest","zero","slinear"]:
#插值函数
f=interpolate.interp1d(df.iloc[:,0],df.iloc[:,1],kind=kind)
#插值
ynew=f(xnew)
#绘制插值数据曲线
plt.plot(xnew,ynew,label=str(kind))
plt.legend(loc="lower right")
<matplotlib.legend.Legend at 0x1276e98c2b0>
原采样数据在x轴为非等间隔的,通过interpolate.interp1d插值后,变成了等间隔的数据,且在这种情况下使用线性插值效果较好