我想知道是否有人可以给我找到两个等高线图交集的线索?理想情况下,它将采用一对轮廓,然后返回交点的坐标
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()
到目前为止,我尝试提取构成轮廓的坐标并尝试找到两个轮廓的数组的交集,但它返回一个空数组,我假设因为没有作为两个数组成员的精确坐标
Edit:
我做到了。就是这样:
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)
可以通过以下方式访问坐标
intersection_example.x ##get x points
intersection_example.y ##get y points
list(intersection_example.coords) ## get in [x,y] formatting
希望它可以帮助某人