【问题标题】:find X-axis data points where horizontal line passes through on respective Y-axis value在相应的 Y 轴值上找到水平线穿过的 X 轴数据点
【发布时间】:2018-07-20 21:34:45
【问题描述】:

我在x-axis 上有一个[1,2,3,4,5] 数据点,在y-axis 上有其各自的值,例如[10,15,10,10,20]

通常通过给定的x-axis 数据点找到y-axis 的值点 就像y=f(x),我检查了这个,我们可以通过interpolation 使用numpy 来实现这一点。但我没有找到如何通过给定的y 轴值插入x 轴.. 根据我想要的附加屏幕找到线 12 相交的相应 x 轴值..
所以我期待在 x-axis 上得到类似 [1, 1.x, 2, 2.x, 3, 4, 4.x, 5, 5.x] 的结果

【问题讨论】:

    标签: python pandas numpy scipy interpolation


    【解决方案1】:

    如果是平滑曲线,可以使用InterpolatedUnivariateSpline

    import numpy as np
    from scipy import interpolate
    
    x = np.linspace(0, 20, 100)
    y = np.sin(x + 0.1)
    
    y0 = 0.3
    spline = interpolate.InterpolatedUnivariateSpline(x, y - y0)
    
    xp = spline.roots()
    

    剧情如下:

    pl.plot(x, y)
    pl.axhline(0.3, color="black", linestyle="dashed")
    pl.vlines(xp, 0, 0.3, color="gray", linestyle="dotted")
    

    如果你想要线性插值:

    x = np.linspace(0, 20, 20)
    y = np.sin(x + 0.1)
    y0 = 0.3
    y_offset = y - y0
    pos = np.where((y_offset[1:] * y_offset[:-1]) <= 0)[0]
    
    x1 = x[pos]
    x2 = x[pos+1]
    y1 = y[pos]
    y2 = y[pos+1]
    
    xp = (y0 - y1) / (y2 - y1) * (x2 - x1) + x1
    

    【讨论】:

      【解决方案2】:

      如果您将interp1d(x,y) 更改为interp1d(y,x),您将x 表示为y 的函数。

      请注意,如果 f(x) 不是唯一的,您可能会遇到意外或未定义的行为。

      【讨论】:

        猜你喜欢
        • 2023-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多