title: Is it possible to use im.set_clip_path twice on two different images?
body: <p>I'm messing around with some code I found <a href="https://tutorialmeta.com/question/filling-in-the-area-between-two-lines-with-a-custom-color-gradient" rel="nofollow noreferrer">here</a>, which plots a color gradient between two lines. I've adjusted it so it can deal with intersections, but am trying to figure out how to also get a color gradient below the bottom line (in the correct color of course). I feel like the issue is with the second <code>gradient.set_clip_path(polygon.get_paths()[0], transform=ax.transData)</code> in the fun_create_gradient_beneath_bottom_line function, because when I delete that line it does show the entire rectangular gradient. Is this not possible? Or is there another way I should approach this?</p>
  <pre><code>import matplotlib.pyplot as plt
  from matplotlib.colors import LinearSegmentedColormap
  import numpy as np
  
  
  def fun_create_gradient_between_lines(ax, x, top_y, bottom_y, cmap_top, cmap_bottom, type_int, x_min=0, x_max=0):
      &quot;&quot;&quot;
      param: type_int -&gt; int can take on 
                         0 - no intersections
                         1 - normal intersections
                         2 - chunk after last intersection
      &quot;&quot;&quot;
      polygon = plt.fill_between(x, top_y, bottom_y, lw=0, color='none')
      verts = np.vstack([p.vertices for p in polygon.get_paths()])
      
      if type_int == 0:
          gradient = plt.imshow(np.linspace(0, 1, 256).reshape(-1, 1), cmap=cmap_top, aspect='auto', origin='lower',
                            extent=[verts[:, 0].min(), verts[:, 0].max(), verts[:, 1].min(), verts[:, 1].max()])
          gradient.set_clip_path(polygon.get_paths()[0], transform=ax.transData)
  
          fun_create_gradient_beneath_bottom_line(ax, x, bottom_y, cmap_bottom, type_int, verts[:, 0].min(), verts[:, 0].max())
      elif type_int == 1:
          gradient = plt.imshow(np.linspace(0, 1, 256).reshape(-1, 1), cmap=cmap_top, aspect='auto', origin='lower',
                            extent=[x_min, x_max, verts[:, 1].min(), verts[:, 1].max()])
          gradient.set_clip_path(polygon.get_paths()[0], transform=ax.transData)
  
          fun_create_gradient_beneath_bottom_line(ax, x, bottom_y, cmap_bottom, type_int, x_min, x_max)
      elif type_int == 2:
          gradient = plt.imshow(np.linspace(0, 1, 256).reshape(-1, 1), cmap=cmap_top, aspect='auto', origin='lower',
                            extent=[x_min, max(x), verts[:, 1].min(), verts[:, 1].max()])
          gradient.set_clip_path(polygon.get_paths()[0], transform=ax.transData)
  
          fun_create_gradient_beneath_bottom_line(ax, x, bottom_y, cmap_bottom, type_int, x_min, max(x))
      else:
          print(&quot;Please select type 0, 1, or 2 for the gradient between the lines.&quot;)
          
  
  
  
  def fun_create_gradient_beneath_bottom_line(ax, x, bottom_y, cmap_bottom, type_int, x_min=0, x_max=0):
      # TODO: doesn't work yet. Issue lies in the .set_clip_path of the polygons (I think)
      polygon = plt.fill_between(0, bottom_y, lw=0, color='none')
  
      if type_int == 0:
          gradient = plt.imshow(np.linspace(0, 1, 256).reshape(-1, 1), cmap=cmap_bottom, aspect='auto', origin='lower',
                            extent=[min(x), max(x), min(bottom_y), max(bottom_y)])
      elif type_int == 1:
          gradient = plt.imshow(np.linspace(0, 1, 256).reshape(-1, 1), cmap=cmap_bottom, aspect='auto', origin='lower',
                            extent=[x_min, x_max, min(bottom_y), max(bottom_y)])
      elif type_int == 2:
          gradient = plt.imshow(np.linspace(0, 1, 256).reshape(-1, 1), cmap=cmap_bottom, aspect='auto', origin='lower',
                            extent=[x_min, x_max, min(bottom_y), max(bottom_y)])
      else:
          print(&quot;Please select type 0, 1, or 2 for the gradient beneath bottom line.&quot;)
          
      gradient.set_clip_path(polygon.get_paths()[0], transform=ax.transData)
  
  
  playerlevel_color = '#6b2b91'
  averageplayerlevel_color = '#4993e9'
  
  x = np.linspace(0, 10, 200)
  y1 = np.random.normal(0.05, 1, 200).cumsum() + 40
  y2 = np.random.normal(0.05, 1, 200).cumsum() + 50
  cm1 = LinearSegmentedColormap.from_list(&quot;Average Player Level&quot;, [&quot;white&quot;, averageplayerlevel_color])
  cm2 = LinearSegmentedColormap.from_list(&quot;Target Player Level&quot;, [&quot;white&quot;, playerlevel_color])
  
  fig, ax = plt.subplots()
  ax.plot(x, y1, color=playerlevel_color)
  ax.plot(x, y2, color=averageplayerlevel_color)
  
  xlim = (x.min(), x.max())
  ylim = ax.get_ylim()
  
  list_intersections = []
  for index in range(1, len(x)):
      if ((y1[index] - y2[index] &gt; 0) &amp; (y1[index - 1] - y2[index - 1] &gt; 0)) | ((y1[index] - y2[index] &lt; 0) &amp; (y1[index - 1] - y2[index - 1] &lt; 0)):
          continue
      else:
          list_intersections.append(index)
      
  print(list_intersections)
  
  top_line = 1 if y1[0] &gt; y2[0] else 0
  x_min = 0
  if len(list_intersections) == 0:
      if top_line:
          fun_create_gradient_between_lines(ax, x, y1, y2, cm2, cm1, 0)
      else:
          fun_create_gradient_between_lines(ax, x, y2, y1, cm1, cm2, 0)
  else:
      for intersection_index in list_intersections:
          if top_line:
              fun_create_gradient_between_lines(ax, x, y1, y2, cm2, cm1, 1, x_min, x[intersection_index])
          else:
              fun_create_gradient_between_lines(ax, x, y2, y1, cm1, cm2, 1, x_min, x[intersection_index])
              
          top_line = 1 - top_line
          x_min = x[intersection_index]
          
      # Chunk after last intersection  
      if top_line:
          fun_create_gradient_between_lines(ax, x, y1, y2, cm2, cm1, 2, x_min)
      else:
          fun_create_gradient_between_lines(ax, x, y2, y1, cm1, cm2, 2, x_min)        
      
  ax.set_xlim(xlim)
  ax.set_ylim(ylim)
  plt.show()
  </code></pre>