python-3.x matplotlib:获取精确的交点

mzsu5hc0  于 2023-02-26  发布在  Python
关注(0)|答案(4)|浏览(180)
import numpy as np
import matplotlib.pyplot as plt

x = [1,2]
y1 = [11, 0]
y2 = [0, 5]

np_x = np.array(x)
np_y1 = np.array(y1)
np_y2 = np.array(y2)

idx = np.argwhere(np.diff(np.sign(np_y2 - np_y1))).flatten()

isect = zip(np_x[idx], np_y1[idx])

for x,y in isect:
    print(f'({x}, {y})')

plt.plot(np_x, np_y1)
plt.plot(np_x, np_y2)
plt.show()

下面是上述代码的图形.然而,打印的交叉点是(1,11)
我想是因为它被四舍五入成整数。
我怎样才能得到精确的交点?(如(1.7,3.8)
谢谢

13z8s7eq

13z8s7eq1#

对数据进行插值以获得更多点,从一个y数组中减去另一个y数组,计算每个点的差值符号,检测符号何时发生变化,获取此变化的索引:这就是交集索引。
编辑:我的答案和其他两个答案的区别在于,如果你的数据不是仿射的,我的答案仍然有效。

import numpy as np
import matplotlib.pyplot as plt
N = 1000

x = np.array([1, 2])
y1 = np.array([11, 0])
y2 = np.array([0, 5])

fig, ax = plt.subplots()
ax.plot(x, y1)
ax.plot(x, y2)
fig.show()

xi = np.linspace(x[0], x[-1], N)
yi1 = np.interp(xi, x, y1)
yi2 = np.interp(xi, x, y2)
difference_sign = np.sign(yi1 - yi2)
sign_variation = np.diff(difference_sign)
intersection_index = np.argmax(sign_variation != 0)
xinter, yinter = xi[intersection_index], yi1[intersection_index]

ax.scatter(xinter, yinter, marker="D", zorder=3)

wribegjk

wribegjk2#

两条直线的交点应该是可以用数学方法求解的,它是(-(np_x[0]*(np_y1[1]-np_y2[1]) + np_x[1]*(np_y2[0]-np_y1[0]))/(np_y1[0]-np_y1[1]-np_y2[0]+np_y2[1]), -(y1[1]*y2[0]-y1[0]*y2[1])/(np_y1[0]-np_y1[1]-np_y2[0]+np_y2[1]))

qyzbxkaa

qyzbxkaa3#

您需要使用更密集的数组值来获得更精确的结果。

import numpy as np
import matplotlib.pyplot as plt

# Line1, (x, y1)
x = [1, 2]
y1 = [11, 0]

# Line2, (x, y2), common x
y2 = [0, 5]

# Create denser array values, here 100 items for each line
np_x = np.linspace(x[0], x[1], 100)
np_y1 = np.linspace(y1[0], y1[1], 100)
np_y2 = np.linspace(y2[0], y2[1], 100)

# Find the indices of array elements that are non-zero, grouped by element.
idx = np.argwhere(np.diff(np.sign(np_y2 - np_y1))).flatten()

isect = zip(np_x[idx], np_y1[idx])

for xi, yi in isect:
    print(f'({xi}, {yi})')
    plt.scatter(xi, yi, color="red", s=50)

plt.plot(np_x, np_y1)
plt.plot(np_x, np_y2)
plt.show()

输出:

(1.6868686868686869, 3.4444444444444446)

用于检查的图形输出:

ohtdti5x

ohtdti5x4#

    • 概述**

您可以通过查找与您提供的每对点相交的每个线性函数的斜率和交点来分析结果。* * x = 27/16 = 1.6875****y = 55/16 = 3.4375**。

    • 使用symy查找精确的交集**
import sympy as sp
import numpy as np
import matplotlib.pyplot as plt

def slope(X, Y):
    """Find the slope of the line that crosses two points"""
    return (Y[1] - Y[0])/(X[1] -X[0])

# provided data
x0 = np.array([1, 2])
y1 = np.array([11, 0])
y2 = np.array([0, 5])

# symbols to solve equations
x = sp.Symbol('x')
y = sp.Symbol('y')
b = sp.symbols('b')

# slopes
M1, M2 = slope(x0, y1), slope(x0, y2)

# solving for the intersection of each line to y-axis
# i.e, solving 'y = M*x + b' for b, for each line
b1 = sp.solve(sp.Eq(M1*x0[0] + b, y1[0]))
b2 = sp.solve(sp.Eq(M2*x0[0] + b, y2[0]))

# solving for the intersection x between the two lines
# then finding y(x)
x_intersec = sp.solve(sp.Eq(M1*x + b1[0], M2*x + b2[0]))
y_intercec = M1*x_intercept[0] + b1[0]
print(x_intersec[0], y_intersec)

# result: (1.68750000000000, 3.43750000000000)

# ploting
plt.plot(x0, y1)
plt.plot(x0, y2)
plt.scatter(x_intercept[0], y_intercept, color="red", s=60, zorder=3)
plt.show()

相关问题