title: Matplotlib remove all patches from figure
body: <p>I am trying to plot various rectangles on top of an image stream. Before displaying the next image, all former rectangles should be removed again.</p>
  
  <p>From <a href="https://stackoverflow.com/questions/21687571/matplotlib-remove-patches-from-figure">this question</a> I found a first possible solution. Below is a simplified example of what I am doing at the moment.</p>
  
  <pre><code>fig, ax = plt.subplots(1)
  im = ax.imshow(np.zeros((800, 800, 3)))
  
  for i in range(100):
      img = plt.imread('my_image_%03d.png' % i)
      im.set_data(img)
  
      rect_lst = getListOfRandomRects(n='random', dim=img.shape[0:2])
      patch_lst = [patches.Rectangle((r[0], r[1]), r[2], r[3],
                                     linewidth=1, facecolor='none')
                   for r in rect_lst]
      [ax.add_patch(p) for p in patch_lst]
      plt.pause(0.1)
      plt.draw()
      [p.remove() for p in patch_lst]
  </code></pre>
  
  <p>What I don't like about this is that I have to keep track of of the <code>patch_lst</code> in order to remove them again. I would prefer to simply remove all patches and get something like this:</p>
  
  <pre><code>for i in range(100):
      [...]
  
      rect_lst = getListOfRandomRects(n='random', dim=img.shape[0:2])
      [ax.add_patch(patches.Rectangle((r[0], r[1]), r[2], r[3],
                                      linewidth=1, facecolor='none'))
                   for r in rect_lst]
  
      plt.pause(0.1)
      plt.draw()
      ax.clear_all_patches()       # &lt;-- this is what I am looking for
  </code></pre>
  
  <p>I did try <code>ax.clear()</code>, however this also removes the underlying image. Any suggestions?</p>