InterpolatedUnivariateSpline和ax.fill_between产生具有低Y值的意外结果(填充错误区域)

时间:2018-05-21 13:14:40

标签: numpy matplotlib scipy curve-fitting

我有一个函数应该采用一些原始数据,将其绘制到画布上,然后填充基线和预定义峰值之间的区域,这对于高Y值很有效,但是当得到相反的结果时使用低Y值。我的问题是双重的:

  1. 为什么会这样?
  2. 解决此问题的有效方法是什么(我尝试将所有Y值乘以1E6,然后执行InterpolatedUnivariateSpline拟合,然后再次将返回拟合除以1E6,但必须有更好的方法来解决此问题。)< / LI>

    段:

    X = [16.08278,16.090878,16.098978,16.107077,16.115177,16.123279,16.13138,16.139482,16.147586,16.155689,16.163793,16.171899,16.180004,16.18811,16.196218,16.204325,16.212433,16.220543,16.228652,16.236762,16.244874,16.252985,16.261097,16.269211,16.277324,16.285439,16.293554,16.30167,16.309786,16.317904,16.326021,16.334139,16.342259,16.350379,16.358499,16.366621,16.374742]
    Y = [1.496555,1.766111,2.074339,2.426317,2.825952,3.274024,3.764088,4.288722,4.839724,5.406741,5.978055,6.536869,7.064041,7.540824,7.948076,8.267242,8.48543,8.596198,8.598762,8.492928,8.279867,7.962899,7.55062,7.059239,6.508092,5.91964,5.318298,4.7234,4.148229,3.602356,3.094568,2.635609,2.231337,1.882143,1.58295,1.328678,1.113859]
    Y2 = [1496555,1766111,2074339,2426317,2825952,3274024,3764088,4288722,4839724,5406741,5978055,6536869,7064041,7540824,7948076,8267242,8485430,8596198,8598762,8492928,8279867,7962899,7550620,7059239,6508092,5919640,5318298,4723400,4148229,3602356,3094568,2635609,2231337,1882143,1582950,1328678,1113859]
    
    # Toggle low vs high Y-values
    #Y = Y2
    
    import matplotlib.pyplot as plt
    import numpy as np
    from scipy.interpolate import InterpolatedUnivariateSpline
    
    fig = plt.figure(figsize=(8, 6))
    ax = fig.add_subplot(111)
    plt.plot(X, Y, 'b-')
    plt.legend(['Raw Data'], loc='best')
    plt.xlabel("Retention Time [m]")
    plt.ylabel("Intensity [au]")
    newTime = np.linspace(X[0], X[-1], len(X))
    f = InterpolatedUnivariateSpline(X, Y)
    newIntensity = f(newTime)
    ax.fill_between(X, newTime, newIntensity, alpha=0.5)
    plt.show(fig)
    

    这产生以下数字:

    enter image description here 这就是我所期望的(并且在高Y值时出现)。

    enter image description here 这种情况发生时Y值较低。

1 个答案:

答案 0 :(得分:1)

我很抱歉这么快回答我自己的问题,但我注意到我在最初实现这个以前从未提出过的问题时犯了一个错误,因为我总是有高强度数据。

ax.fill_between期望xy1y2,并且对于高Y值数据,它开始填充区域不是从0开始,而是从X-开始值。由于尺度差异,这并不是显而易见的,只有在切换到低Y值后才变得明显。只需将ax.fill_between(X, newTime, newIntensity, alpha=0.5)更改为ax.fill_between(X, 0, newIntensity, alpha=0.5)即可获得预期结果。