"""
scipy3.0:插值。与之前写的B样条插值差不多
马玉华 2019.11.7
"""
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
x = np.linspace(0,1,10) # 0-1均匀取10个数
y = np.sin(2*np.pi*x)
func = interp1d(x,y,kind='cubic') # cubic三阶B样条插值
x_new = np.linspace(0,1,50)
y_new = func(x_new)
print(y_new)
plt.figure()
plt.plot(x,y,'r')
plt.plot(x_new,y_new,'k')
plt.show()
输出结果:
"C:\Users\mayuhuaw\Desktop\Python数据分析-基础技术\3. scipy\venv\Scripts\python.exe" "C:/Users/mayuhuaw/Desktop/Python数据分析-基础技术/3. scipy/scipy3.0:插值.py"
[ 0.00000000e+00 1.31268478e-01 2.58058896e-01 3.79095818e-01
4.93103803e-01 5.98807414e-01 6.94931212e-01 7.80199759e-01
8.53337617e-01 9.13069347e-01 9.58119510e-01 9.87213058e-01
9.99462682e-01 9.95096409e-01 9.74541587e-01 9.38225564e-01
8.86575689e-01 8.20078818e-01 7.39913600e-01 6.47705004e-01
5.45085433e-01 4.33687295e-01 3.15143957e-01 1.91245093e-01
6.41081509e-02 -6.41081509e-02 -1.91245093e-01 -3.15143957e-01
-4.33687295e-01 -5.45085433e-01 -6.47705004e-01 -7.39913600e-01
-8.20078818e-01 -8.86575689e-01 -9.38225564e-01 -9.74541587e-01
-9.95096409e-01 -9.99462682e-01 -9.87213058e-01 -9.58119510e-01
-9.13069347e-01 -8.53337617e-01 -7.80199759e-01 -6.94931212e-01
-5.98807414e-01 -4.93103803e-01 -3.79095818e-01 -2.58058896e-01
-1.31268478e-01 -2.44929360e-16]
进程已结束,退出代码 0