[英]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. 我首先在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. 然后,我只想沿轮廓线提取x,y坐标,并将它们存储在x和y变量中,以在下一个操作中使用它们。 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. 我在论坛中看到了一个代码,用于提取沿轮廓的坐标,但是并没有给我要存储在单独的x和y变量中的点,因此我可以在下一个操作中使用它们。
Please help me if there is an easy python code possible for this operation. 如果此操作有简单的python代码,请帮助我。 I am not expert in python. 我不是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. 我已经尝试了论坛中的链接,但没有像xy那样给我坐标,它带有带括号的x和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. 使用此代码,我得到包含x和y的输出路径[i]放在方括号内。 I want to save the output x and y coordinates as an array. 我想将输出x和y坐标保存为数组。 The output data looks like, 输出数据看起来像
[[-0.0018 -0.02222131]] [[-0.0018 -0.02222131]]
But I want them as x=-0.0018 y= -0.02222131 Thanks, 但我希望它们为x = -0.0018 y = -0.02222131,谢谢,
I just tried with for ip,path in enumerate(contours): for i,item in enumerate(path): print(item[i][0],item[i][1]) 我刚刚尝试使用ip,enumerate(contours)中的路径:对于i,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" 但这并没有给我x和y值的列表,而是显示错误“ IndexError:索引1超出了轴1的大小1的范围”
If path[i]
gives [[-0.0018 -0.02222131]]
then 如果path[i]
给出[[-0.0018 -0.02222131]]
则
x = path[i][0][0]
y = path[i][0][1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.