title: Finding intersection of two contour plots in Python
body: <p>I was wondering if anyone could give me a clue on finding the intersection of two contour plots? Ideally it would take pair of contours, then return the coordinate of the intersections</p>
  
  <pre><code>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()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/e94gB.png" rel="noreferrer"><img src="https://i.stack.imgur.com/e94gB.png" alt="enter image description here"></a></p>
  
  <p>So far I tried, extracting the coordinate that makes up the contour and try to find the intersection of the arrays of both contours, but it returns an empty array, I assume since there isn't an exact coordinate which is a member of both arrays</p>
  
  <p>Edit:</p>
  
  <p>I did it. Here's how:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>where coordinates can be accessed by</p>
  
  <pre><code>intersection_example.x ##get x points
  intersection_example.y ##get y points
  list(intersection_example.coords)  ## get in [x,y] formatting
  </code></pre>
  
  <p>hope it helps someone</p>