Python 从与0.0标高相交的直线获取值
问题是从与0.0标高相交的红线(黑线)中查找值。红线绘制如下:Python 从与0.0标高相交的直线获取值,python,geometry,Python,Geometry,问题是从与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.440
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\u行截距=-c/m=x
为了将变量转换为y=mx+c
形式,使用点斜率形式:y-y1=m(x-x1)
,其中y1
和x1
是直线(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
回答得很好,非常感谢。在这里发布之前,我想知道斜率并从数学上得到该值,我发现了这个链接
https://nessy.info/?p=16
但无法实现它,我是Python新手,现在我了解了如何使用您的示例。万分感谢!