简体   繁体   中英

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()

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

Going through the collections and extracting the paths and vertices is not the most straight forward or fastest thing to do. The returned Contour object actually has attributes for the segments via cs.allsegs , which returns a nested list of shape [level][element][vertex_coord]:

num_levels = len(cs.allsegs)
num_element = len(cs.allsegs[0])  # in level 0
num_vertices = len(cs.allsegs[0][0])  # of element 0, in level 0
num_coord = len(cs.allsegs[0][0][0])  # of vertex 0, in element 0, in level 0

Hence the vertices of an all paths can be extracted as:

cs.allsegs[i][j]  # for element j, in level i

See reference: https://matplotlib.org/3.1.1/api/contour_api.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM