简体   繁体   English

从matplotlib中的轮廓获取坐标?

[英]Get coordinates from the contour in matplotlib?

Background 背景

From the documentation example here , one can easily produce the following contour plot with the code snippet. 这里的文档示例中 ,可以使用代码片段轻松生成以下等高线图。

import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

# Create a simple contour plot with labels using default colors.  The
# inline argument to clabel will control whether the labels are draw
# over the line segments of the contour, removing the lines beneath
# the label
plt.figure()
CS = plt.contour(X, Y, Z)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('Simplest default with labels')

在此输入图像描述

My Goal 我的目标

I have obtained my contour plot and meanwhile got the matplotlib.contour.QuadContourSet instance CS . 我已经获得了我的等高线图,同时得到了matplotlib.contour.QuadContourSet实例CS In the example snippet, CS is only used for clabel() . 在示例代码段中, CS仅用于clabel() However for my case, I need to obtain either the equation of the contour line or the coordinate set for further computation. 但是对于我的情况, 我需要获得轮廓线的方程或坐标集以进一步计算。

How can I extract the coordinates of the contour line from the instance CS ? 如何从实例CS提取轮廓线的坐标? OR How can I achieve it in other ways? 或者我怎样才能以其他方式实现它?

I bet there must be a way of doing so. 我打赌必须有办法这样做。 Otherwise, the contour thing is only a "vase for visualization" then. 否则,轮廓事物只是一个“可视化的花瓶”。

You can get the coordinates of the contours from the CS.allsegs list. 您可以从CS.allsegs列表中获取轮廓的坐标。

Try: 尝试:

dat0= CS.allsegs[0][0]
plt.plot(dat0[:,0],dat0[:,1])

to plot the first (-1) contour level. 绘制第一个(-1)轮廓水平。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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