在 Python 中查找两个等高线图的交点

blu*_*ood 7 python matplotlib contour scipy

我想知道是否有人能给我一个关于找到两个等高线图的交点的线索?理想情况下,它需要一对轮廓,然后返回交叉点的坐标

Z1 = somefunction
Z2 = somefunction1
Z3 = somefunction2
xlist = np.linspace(0, 10, 50)
ylist = np.linspace(0, 10, 50)
X, Y = np.meshgrid(xlist, ylist)
figtest = plt.figure()
axtest = figtest.add_subplot( 111 )
axtest.contour(X,Y,Z1,[1],colors='green', linewidths=4)
axtest.contour(X,Y,Z2,[1],colors='orange', linewidths=4)
axtest.contour(X,Y,Z3,[1],colors='blue',linewidths=4)                  
axtest.set_xlim([0, 9])
axtest.set_ylim([0, 9])
axtest.legend()
axtest.grid()
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

到目前为止,我尝试提取构成轮廓的坐标并尝试找到两个轮廓的数组的交集,但它返回一个空数组,我假设因为没有精确的坐标是两个数组的成员

编辑:

我做到了。就是这样:

from shapely import geometry

def findIntersection(contour1,contour2):
  p1 = contour1.collections[0].get_paths()[0]
  v1 = p1.vertices

  p2 = contour2.collections[0].get_paths()[0]
  v2 = p2.vertices

  poly1 = geometry.LineString(v1)
  poly2 = geometry.LineString(v2)

  intersection = poly1.intersection(poly2)

  return intersection

c1 = axtest.contour(X,Y,Z1,[1],colors='green', linewidths=4)
c2 = axtest.contour(X,Y,Z2,[1],colors='orange', linewidths=4)

intersection_example = findIntersection(c1,c2)
Run Code Online (Sandbox Code Playgroud)

可以通过以下方式访问坐标

intersection_example.x ##get x points
intersection_example.y ##get y points
list(intersection_example.coords)  ## get in [x,y] formatting
Run Code Online (Sandbox Code Playgroud)

希望它可以帮助某人