从与 0.0 级别相交的线中获取值

Get value from a line intersecting 0.0 level

问题是从与 0.0 级别(黑线)相交的红线中找到值。红线绘制如下:

plt.plot(C, iG, color='r')
plt.plot(C, iG, 'o', color='r', markersize=6)

黑线绘制为:

plt.axhline(y=0, color='k')

变量在这里:

C = [0, 20, 40, 60, 80, 100]
iG = [1.3872346819371657, 0.7872943505903507, 0.17782668886707143, -0.44058186267346144, -1.0673973968907333, -1.7021324469635957]

因此,只有一条线(即形成红线的 6 对 x,y)。

图在这里:

你可以简单地用数学方法解决它。如果取一条线y = mx + c,x截距在y = 0处,所以求解0 = mx + c:

0 = mx + c  
-c = mx  
-c/m = x  

因此在行 y = mx + c x_intercept = -c/m = x

为了将你的变量转换成y = mx + c形式,你使用point-slope形式:y - y1 = m(x - x1) 其中y1x1是坐标你线上的一个点 (x1, y1)。使用您的两个坐标求斜率,然后完成方程式。和以前一样,y = 0:

0 - y1 = m(x - x1)
-y1 = mx - mx1
-y1/m = x - x1
-y1/m + x1 = x

下面是如何做到这一点的代码:

# points inputted as a list of the x and y values of the points
def find_xintercept(point1, point2):

    # slope (m) = rise over run
    slope = (point1[1] - point2[1]) / (point1[0] - point2[0])
    return -point1[1] / slope + point1[0]

示例:

print(find_xintercept([C[0], iG[0]], [C[1], iG[1]]))

打印:

46.24575510110953