【问题标题】:Python: Finding intersection point of two lines on semi log scalePython:在半对数刻度上查找两条线的交点
【发布时间】:2020-05-07 16:31:27
【问题描述】:

我在半对数比例图上有两条线。 Line1 是绿色的,而 Line2 是深紫色的。如何找到 Line1(绿色)和 Line2(深紫色)之间的交点?我尝试了基于维基百科link的交集公式,但它返回了红线。

import numpy as np
import matplotlib.pyplot as plt

xmin, xmax = 2000, 7000
ymin, ymax = 10, 50000

#lighting fixture limitation
cct_min = 2700
cct_max = 6000
illu_min = 110
illu_max = 2100

def findIntersection(x1,y1,x2,y2,x3,y3,x4,y4):
        px= ( (x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4) ) / ( (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4) ) 
        py= ( (x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4) ) / ( (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4) )
        return [px, py]

#arrays
x_grid = np.array([2000, 3000, 4000, 5000, 6000, 7000])
x = np.linspace(xmin, xmax)

#plot
fig, ax = plt.subplots(figsize=(8, 8))
ax.set_xlim(xmin=xmin, xmax=xmax)
ax.set_ylim(ymin=ymin, ymax=ymax)
ax.set_yscale('log')
ax.axvline(x=4000, color='g', linestyle='-')
ax.plot([cct_min, cct_max],[illu_min,illu_max], color='darkviolet')
a,b = findIntersection(cct_min,illu_min,cct_max,illu_max,4000,10,4000,10000)
ax.axhline(y=b, color='r', linestyle='-')
plt.show()

谢谢。

【问题讨论】:

    标签: python numpy matplotlib


    【解决方案1】:

    您可以先将所有y 位置转换为对数空间,计算交集,然后取结果pyexp

    在垂直线的情况下,可以简化公式,但以下方法适用于任何类型的线。请注意,所有y 值都必须严格为正。并且输入线不平行。

    def findIntersection(x1, y1, x2, y2, x3, y3, x4, y4):
        y1 = np.log(y1)
        y2 = np.log(y2)
        y3 = np.log(y3)
        y4 = np.log(y4)
        denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
        px = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / denom
        py = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / denom
        return [px, np.exp(py)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-03
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多