import matplotlib.pyplot as plt
import numpy as np
# Dummy x,y,z data.
x = np.linspace(0, 1, 10)
y = np.linspace(0, 1, 10)
x,y = np.meshgrid(x,y)
z = np.sin(7*x)*np.sin(7*y)
# Contours levels.
levels = [0, 0.4, 0.8]
nlevels = len(levels)
# Use either one of the following two lines:
contourset = plt.contour(x, y, z, levels) # Line contours
#contourset = plt.contourf(x, y, z, levels) # Filled contours
# contourset.collections is a list of nlevels LineCollections if using contour
# and a list of (nlevels-1) PathCollections if using contourf.
print contourset.collections
# Look at a single level index. If you want all of the contour levels, you
# will need to loop through them.
level = 1
print 'level is', levels[level]
paths = contourset.collections[level].get_paths()
npaths = len(paths)
print 'number of paths', npaths
for i in range(npaths):
path = paths[i]
print 'path', i, 'of', npaths
# Each path has vertices, it may also have codes. If codes is None, the
# vertices are a single continuous path of line segments and should be easy
# to deal with. If codes is not None then vertices is probably 2 or more
# discontinuous line segments; you will need to understand the codes for
# MOVETO and LINETO at least - see lib/matplotlib/path.py for details.
print ' codes', path.codes
print ' vertices', path.vertices
# Show the contour plot to help explanation.
plt.colorbar()
plt.show()