簡體   English   中英

Python:在半對數刻度上找到兩條線的交點

[英]Python: Finding intersection point of two lines on semi log scale

我在半對數比例圖上有兩條線。 Line1 是綠色的,而 Line2 是深紫色的。 如何找到 Line1(綠色)和 Line2(深紫色)之間的交點? 我嘗試了基於 Wikipedia 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()

兩條線的交點

謝謝。

您可以首先將所有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)]

結果圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM