[SciPy-user] Getting coordinates of a level (contour) curve
John Hunter
jdh2358 at gmail.com
Tue Aug 12 11:08:02 EDT 2008
On Tue, Aug 12, 2008 at 9:36 AM, Rob Clewley <rob.clewley at gmail.com> wrote:
>> Yes, you are right. But what if I have a mixture of gaussians, or any
>> other 2D probability density function?
>
> Indeed. Isn't the question about how to extract the data points for
> the curve from the 'contour' object in matplotlib, in the general
> case? Unfortunately I don't have the answer to that, but maybe
> introspection of the object would lead to an answer. From the API doc
> I see a mysterious attribute called 'level'.
The mpl contour function returns a matplotlib.contour.ContourSet
instance which has an attribute "level" array of levels that the
contours are drawn on
In [57]: CS = plt.contour(X, Y, Z)
In [58]: CS.levels
Out[58]: array([-1. , -0.5, 0. , 0.5, 1. , 1.5])
It also has an equal length list of line collections
(matplotlib.collections.LineCollection) which you can use to extract
the x, y vertices of the contour lines at a given level. For a single
level, the line collection may contain one or more independent lines
in the collections. Here is some example code to get you started:
In [59]: level0 = CS.levels[0]
In [60]: print level0
-1.0
In [61]: c0 = CS.collections[0]
In [62]: paths = c0.get_paths()
In [63]: len(paths)
Out[63]: 1
In [64]: path0 = paths[0]
In [65]: xy = path0.vertices
In [66]: xy.shape
Out[66]: (237, 2)
In [67]: xy[:10,]
Out[67]:
array([[-0.15 , -0.95150169],
[-0.15877627, -0.95 ],
[-0.175 , -0.94720234],
[-0.2 , -0.94221229],
[-0.225 , -0.93652781],
[-0.25 , -0.93013814],
[-0.26810676, -0.925 ],
[-0.275 , -0.9230207 ],
[-0.3 , -0.91514134],
[-0.325 , -0.90651218]])
In [68]:
More information about the SciPy-User
mailing list