Noise in the street
Noise in the street

Reputation: 599

In Python, can Matplotlib's Contour function return the points of a particular contour line?

I had to use contour plots to generate plots of a set of inconveniently defined hyperbolic functions. This works and it plots a series of them with some noise added (it's a passive RF geolocation TDOA problem for those who are familiar with such things).

Since my python program knows the (x,y) of the target, I want to sample all the different hyperbolas that I'm drawing around that point and generate an error ellipse. If I can get the Matplotlib contour function to return the points of each contour line as it is drawn, I can handle the rest of the calculations. So...

Can the Matplotlib contour function return all of the (x,y) values for a particular contour line, say at f(x,y)=0?

Upvotes: 6

Views: 2760

Answers (1)

Falko
Falko

Reputation: 17907

You can draw a contour at a specific level like this:

c = plt.contour(X, Y, Z, [level])

Now you can extract the contour points from the returned object c (see this answer):

v = c.collections[0].get_paths()[0].vertices
x = v[:,0]
y = v[:,1]

Upvotes: 7

Related Questions