title: export matplotlib contour lines to text file
body: <p>I am trying to export the contour lines from matplotlib to a text file. From the information here, I was able to create a MWE, that nearly works.</p>
  <pre><code>def export_contours(fname, cn):
      with open(fname, 'w') as fp:
          # for each contour line
          for v, cc in zip(cn.cvalues, cn.collections):
              if len(cc.get_paths()) == 0:
                  continue
              p = cc.get_paths()[0]
              n = np.size(p.vertices, 0)
              contour = np.hstack([p.vertices, np.ones((n,1))*v])
              np.savetxt(fp, contour, comments='# ',delimiter=',', newline='\n' )
              fp.write('\n')
              
              
  def f(x,y):
      return x**2 + y**2
  
  XX,YY = np.meshgrid(np.linspace(-1, 1,100), np.linspace(-1, 1, 50))
  ZZ = f(np.ravel(XX)[:,None], np.ravel(YY)[:,None]).reshape(XX.shape)
  plt.clf()
  cn = plt.contour(XX,YY,ZZ)       
              
  export_contours('./contour.txt',cn)
  </code></pre>
  <p>Loading this data again in <code>pgfplots</code>, the outer rings seem to be missing.
  I can also see, that for the level 1.75, there are only 10 entries in the text file.</p>
  <pre><code>\begin{tikzpicture}
      \begin{axis}[view={0}{90}]
          \addplot3[contour prepared] table[x index=0, y index=1, z index=2, col sep=comma,] {data/gd_peak_contour.txt};
      \end{axis}
  \end{tikzpicture}
  </code></pre>
  <p><a href="https://i.stack.imgur.com/10C2B.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/10C2B.png" alt="enter image description here" /></a></p>
  <p>Anybody an idea what the problem could be?</p>