使用UnivariateSpline和SCIPY

时间:2014-08-06 02:58:26

标签: python scipy interpolation

我正在尝试使用UnivariateSpline函数生成一些数据(原始布拉格峰)。这是我的代码:

import matplotlib.pyplot as plt
from scipy import interpolate
import numpy as np

spline = interpolate.UnivariateSpline(xi, yi, s = 0)
measured_depth_i = np.linspace(0.4, 10.4, 1000)
measured_dose_i = spline(measured_depth_i)

plt.plot(measured_depth, measured_dose, 'ro', label='Original Points') 
plt.plot(measured_depth_i, measured_depth_i, label='Interpolated Points' ) 

其中xi =

[  0.4 ,   0.48,   0.63,   0.79,   0.93,   1.08,   1.22,   1.38,
         1.53,   1.68,   1.83,   1.99,   2.14,   2.29,   2.43,   2.59,
         2.73,   2.89,   3.04,   3.19,   3.34,   3.48,   3.64,   3.8 ,
         3.94,   4.09,   4.24,   4.39,   4.55,   4.7 ,   4.85,   5.  ,
         5.15,   5.3 ,   5.45,   5.59,   5.75,   5.9 ,   6.05,   6.21,
         6.35,   6.5 ,   6.66,   6.8 ,   6.95,   7.11,   7.24,   7.4 ,
         7.55,   7.71,   7.86,   8.  ,   8.16,   8.31,   8.46,   8.61,
         8.76,   8.91,   9.06,   9.22,   9.36,   9.52,   9.67,   9.81,
         9.97,  10.12,  10.27,  10.4 ]

和yi =

[  2.16457961e+01,   2.16457961e+01,   2.16010733e+01,
         2.18470483e+01,   2.20483005e+01,   2.22942755e+01,
         2.24731664e+01,   2.26967800e+01,   2.29427549e+01,
         2.30545617e+01,   2.32781753e+01,   2.34570662e+01,
         2.36583184e+01,   2.37924866e+01,   2.39713775e+01,
         2.42620751e+01,   2.44856887e+01,   2.46869410e+01,
         2.49552773e+01,   2.51341682e+01,   2.53577818e+01,
         2.56932021e+01,   2.58497317e+01,   2.61404293e+01,
         2.63864043e+01,   2.65876565e+01,   2.68559928e+01,
         2.72361360e+01,   2.74821109e+01,   2.78622540e+01,
         2.81529517e+01,   2.84436494e+01,   2.88461538e+01,
         2.91815742e+01,   2.95617174e+01,   3.00089445e+01,
         3.04114490e+01,   3.09033989e+01,   3.13059034e+01,
         3.18872987e+01,   3.24463327e+01,   3.31395349e+01,
         3.37656530e+01,   3.44364937e+01,   3.51296959e+01,
         3.60017889e+01,   3.68515206e+01,   3.78801431e+01,
         3.91323792e+01,   4.04293381e+01,   4.19946333e+01,
         4.37164580e+01,   4.57513417e+01,   4.83228980e+01,
         5.16100179e+01,   5.58363148e+01,   6.13595707e+01,
         6.93202147e+01,   8.35420394e+01,   1.00000000e+02,
         8.47271914e+01,   3.60688730e+01,   6.21645796e+00,
         3.35420394e-01,   2.23613596e-02,   2.23613596e-02,
         2.23613596e-02,   2.23613596e-02]

我的插值曲线似乎仍然是一条直线,并且根本不匹配我的数据,无论是否改变我的值。任何人都能解释为什么我无法很好地适应我的数据吗?

1 个答案:

答案 0 :(得分:1)

诀窍是在绘图语句中获取变量名称:

spline = interpolate.UnivariateSpline(xi, yi, s = 0)
measured_depth_i = np.linspace(0.4, 10.4, 1000)
measured_dose_i = spline(measured_depth_i)

plt.plot(xi, yi, 'ro', label='Original Points')_
plt.plot(measured_depth_i, measured_dose_i, label='Interpolated Points' )_

结果情节如下: enter image description here