从matplotlib中的等高线获取坐标?

2024-06-28 19:33:58 发布

您现在位置:Python中文网/ 问答频道 /正文

背景

the documentation example here中,可以使用代码片段轻松生成以下等高线图。

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

enter image description here

我的目标

我得到了我的等高线图,同时得到了matplotlib.contour.QuadContourSet实例CS。在示例片段中,CS仅用于clabel()。但是,对于我的情况,我需要获得轮廓线的方程或坐标集,以便进一步计算。

如何从实例中提取等高线的坐标CS?或者 我怎样才能以其他方式实现它呢?

我打赌一定有办法。否则,轮廓只是一个“视觉花瓶”。


Tags: theimportlabelsmatplotlibasnpcmplt