title: Python - convert contours to image
body: <p>I would like to convert to image a subset of contours from an array. I tried this:</p>
  
  <pre><code>z = (plt.contour(data, [0.0, 0.2, 0.4]))
  plt.imshow(z)
  </code></pre>
  
  <p>I get this error</p>
  
  <pre><code>    ...\matplotlib\image.pyc in set_data(self, A)
      428         if (self._A.dtype != np.uint8 and
      429             not np.can_cast(self._A.dtype, np.float)):
  --&gt; 430             raise TypeError("Image data can not convert to float")
      431 
      432         if (self._A.ndim not in (2, 3) or
  
  TypeError: Image data can not convert to float
  </code></pre>
  
  <p>Is there a way around this, or another way to save contours to image?</p>
  
  <p><strong>* UPDATED *</strong></p>
  
  <p>Below is a complete Notebook-style example:</p>
  
  <pre><code># preliminaries
  import matplotlib.pyplot as plt
  import numpy as np
  %matplotlib inline
  
  # create data - from a classic Matplotlib example
  X = np.arange(-5, 5, 0.25)
  Y = np.arange(-5, 5, 0.25)
  X, Y = np.meshgrid(X, Y)
  R = np.sqrt(X**2 + Y**2)
  Z = np.sin(R)
  
  # plot data as image
  # image is displayed correctly
  fig1 = plt.figure(figsize=(6, 6))
  ax = fig1.add_subplot(1, 1, 1)
  ax.set_xticks([])
  ax.set_yticks([])
  plt.imshow(Z, cmap='bone_r')
  
  # plot subset of data as contours
  # contours are displayed correctly
  fig2 = plt.figure(figsize=(6, 6))
  ax = fig2.add_subplot(1, 1, 1)
  z = (plt.contour(Z, [0.0, 0.2, 0.4]))
  
  # overlay - this gives the error message above
  fig3 = plt.figure(figsize=(6, 6))
  ax = fig3.add_subplot(1, 1, 1)
  ax.set_xticks([])
  ax.set_yticks([])
  plt.imshow(Z, cmap='bone_r')
  plt.imshow(z)
  plt.show()
  </code></pre>
  
  <p><strong>UPDATED 2</strong></p>
  
  <p>I used the code below to capture all contours for the desired z level. See accepted answer and comments.</p>
  
  <pre><code>for collection in z.collections:
    paths = collection.get_paths()
    for path in paths:
      line=(path.vertices)`  
      plt.plot(line[:,0], line[:,1], 'k-')
  </code></pre>