我想对我的数据集进行三次样条插值,但绘图只显示一段直线。 但是,如果我只尝试拟合前4组数据,则插值有效。我不知道代码出错的地方。请帮忙!
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import InterpolatedUnivariateSpline
# experimental data
voltage = [0.466, 0.401, 0.302, 0.207, 0.1008,0.0907,0.0805,0.0703,0.0602,
0.0502,0.0421,0.0317,0.0276, -0.08, -0.1, -0.13, -0.16,-0.18,-0.22,
-0.23,-0.27,-0.84,-4.73,-8.13,-10.49,-11.85,-13.,-14.98,-16.29,
-17.07,-18.16]
current = [3.35e-02,1.37E-02,2.50E-03,3.00E-04,8.50E-06,5.90E-06,3.80E-06,
2.60E-06,1.60E-06,1.00E-06,7.00E-07,4.00E-07,3.00E-07,-0.0003,
-0.0004,-0.0005,-0.0006,-0.0007,-0.0008,-0.0009,-0.001,-0.002,
-0.003,-0.004,-0.005,-0.006,-0.007,-0.008,-0.009,-0.01,-0.011]
# cubic splines
xi = np.array(voltage)
yi = np.array(current)
x = np.linspace(xi.min(), xi.max(), len(xi)*100)
sp = InterpolatedUnivariateSpline(xi, yi, k=3)
y = sp(x)
# interpolation
plt.plot(xi, yi, 'go', label = 'original data', markersize = 7)
plt.plot(x,y)