简体   繁体   中英

How to extract coordinates from contour lines in python and store them in new x and y variables?

I am facing a problem in extracting coordinates from a contour plot. I first plotted contour line in python. Then I want to extract only the x,y coordinates along the contour line and store them in x and y variables to use them in the next operation. I saw a code to extract the coordinates along the contour in the forum but that does not give me points to store in separate x and y variable so that I can use them in the next operation.

Please help me if there is an easy python code possible for this operation. I am not expert in python.

I have tried a link from the forum but that does not give me coordinates like xy it comes with bracketed x and y. I cannot write the code here as it shows some error in this editing window.

def get_contour_verts(cp):
    contours=[]
    for cc in cp.collections:
        paths=[]
        for pp in cc.get_paths():
            xy=[]
            for vv in pp.iter_segments():
                xy.append(vv[0])
            paths.append(np.vstack(xy))
        contours.append(paths)
    return contours

contours=get_contour_verts(cp)
      for ip,path in enumerate(contours):
        for i,item in enumerate(path):

           print(path[i])

Using this code I get output path[i] containing x and y within brackets together. I want to save the output x and y coordinates as an array. The output data looks like,

[[-0.0018 -0.02222131]]

But I want them as x=-0.0018 y= -0.02222131 Thanks,

I just tried with for ip,path in enumerate(contours): for i,item in enumerate(path): print(item[i][0],item[i][1])

But it is not giving me a list of x and y values but showing error "IndexError: index 1 is out of bounds for axis 0 with size 1"

If path[i] gives [[-0.0018 -0.02222131]] then

 x = path[i][0][0]
 y = path[i][0][1] 

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