title: Matplotlib contour plot with intersecting contour lines
body: <p>I am trying to make a contour plot of the following data using matplotlib in python. The data is of this form -</p>
  
  <pre><code># x       y      height
    77.23  22.34     56
    77.53  22.87     63
    77.37  22.54     72
    77.29  22.44     88
  </code></pre>
  
  <p>The data actually consists of nearly 10,000 points, which I am reading from an input file. However the set of distinct possible values of z is small (within 50-90, integers), and I wish to have a contour lines for every such distinct z.</p>
  
  <p>Here is my code -</p>
  
  <pre><code>import matplotlib
  import numpy as np
  import matplotlib.cm as cm
  import matplotlib.mlab as mlab
  import matplotlib.pyplot as plt
  import csv
  import sys
  
  # read data from file
  data = csv.reader(open(sys.argv[1], 'rb'), delimiter='|', quotechar='"')
  x = []
  y = []
  z = []
  
  for row in data:
      try:
          x.append(float(row[0]))
          y.append(float(row[1]))
          z.append(float(row[2]))
      except Exception as e:
          pass
          #print e
  
  X, Y = np.meshgrid(x, y)        # (I don't understand why is this required)
  
  # creating a 2D array of z whose leading diagonal elements
  # are the z values from the data set and the off-diagonal
  # elements are 0, as I don't care about them.
  z_2d = []
  default = 0
  for i, no in enumerate(z):
      z_temp = []
      for j in xrange(i): z_temp.append(default)
      z_temp.append(no)
      for j in xrange(i+1,  len(x)): z_temp.append(default)
      z_2d.append(z_temp)
  Z = z_2d
  
  CS = plt.contour(X, Y, Z, list(set(z)))
  plt.figure()
  CB = plt.colorbar(CS, shrink=0.8, extend='both')
  plt.show()
  </code></pre>
  
  <p>Here is the plot of a small sample of data - 
  <img src="https://i.stack.imgur.com/q4M2p.png" alt="enter image description here"></p>
  
  <p>Here is a close look to one of the regions of the above plot (note the overlapping/intersecting lines) - 
  <img src="https://i.stack.imgur.com/bvYtV.jpg" alt="enter image description here"></p>
  
  <p>I don't understand why it doesn't look like a contour plot. The lines are intersecting, which shouldn't happen. What can be possibly wrong? Please help.</p>