【问题标题】:How to find instersection points of multiple lines in matplolib plot如何在matplotlib图中找到多条线的交点
【发布时间】:2019-08-30 14:09:45
【问题描述】:

我有这段代码来绘制多条线(图 1)。我怎样才能找到这些线的交点?

import matplotlib.pylab as pyl
import numpy as np
import math

x = [200, 300, 300, 200,200]
y = [150, 150, 100, 100,140]
x1 = [100, 400]
y1 = [50, 250]

pyl.plot(x, y, 'r')
pyl.plot(x1, y1, 'c')

pyl.xlim(0, 480)
pyl.ylim(0, 320)
pyl.grid(True)
pyl.show()

【问题讨论】:

标签: python matplotlib intersection point


【解决方案1】:

我喜欢Shapely解决这类问题

from shapely.geometry import LineString, Polygon

x = [200, 300, 300, 200, 200]
y = [150, 150, 100, 100, 140]

x1, x2 = [100, 400]
y1, y2 = [50, 250]

poly = Polygon([(x, y) for x, y in zip(x, y)])
line = LineString([(x1, y1), (x2, y2)])

cross = poly.intersection(line)
[px1, px2], [py1, py2] = cross.coords.xy
p1 = (px1, py1) # (200, 116,66)
p2 = (px2, py2) # (250, 150)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    相关资源
    最近更新 更多