Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the (x,y) values of the line that is plotted by a contour plot?

Is there an easy way to get the (x,y) values of a contour line that was plotted like this:

import matplotlib.pyplot as plt
x = [1,2,3,4]
y = [1,2,3,4]
m = [[15,14,13,12],[14,12,10,8],[13,10,7,4],[12,8,4,0]]
cs = plt.contour(x,y,m, [9.5])
plt.show()
like image 607
Philipp der Rautenberg Avatar asked Oct 13 '09 13:10

Philipp der Rautenberg


People also ask

How do you plot a contour graph?

A contour plot is a graphical technique for representing a 3-dimensional surface by plotting constant z slices, called contours, on a 2-dimensional format. That is, given a value for z, lines are drawn for connecting the (x,y) coordinates where that z value occurs.

How do you find the contour line of a function?

If we have a formula for a function z = f(x, y), then we can find the equations for the contours easily. Each contour is obtained by slicing the surface with the horizontal plane z = c, so the equation for the contour at height c is simply f(x, y) = c. (b) f(x, y) = cos√x2 + y2.


1 Answers

Look at the collections property of the returned ContourSet. In particular the get_paths() method of the first collection returns paired points making up each line segment.

cs.collections[0].get_paths()

To get a NumPy array of the coordinates, use the Path.vertices attribute.

p1 = cs.collections[0].get_paths()[0]  # grab the 1st path
coor_p1 = p1.vertices
like image 118
Mark Avatar answered Oct 06 '22 04:10

Mark