2016-11-29 85 views
1

我想對我的數據集做三次樣條插值,但繪圖只顯示一段直線。 但是,如果我只嘗試擬合前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) 

回答

0

問題是,根據documentation,x值需要增加。他們不在你的情況。

(PS:隨時給我一個更新的報告,如果這可以修復你的一切)

+1

非常感謝!我改變了數據點的順序,並且插值工作。 – RiceSweet