On 28 July 2010 01:31, Phil Rosenfield <philr...@astro.washington.edu>wrote:

> I'd like to use the polygons contour makes but I can't figure out how
> to get them from ContourSet. Any examples or links to helpful
> information would be excellent.
>

Attached is an example of how to extract the polygons from a ContourSet.
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()
------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to