由于不再支持matplotlib._cntr,您可以使用skimage 中的find_contour() 函数。这是从documentation 的分析函数中提取轮廓级别 0.8 的简单代码。
import numpy as np
from skimage import measure
# Construct some test data
x, y = np.ogrid[-np.pi:np.pi:100j, -np.pi:np.pi:100j]
r = np.sin(np.exp((np.sin(x)**3 + np.cos(y)**2)))
# Find contours at a constant value of 0.8
contours = measure.find_contours(r, 0.8)
这将为您提供沿轮廓的(行、列)坐标函数中的轮廓,而不是x 和y 的值。要转换为 x 和 y 值,您可以使用来自 scipy 的 interp1d 进行插值:
from scipy.interpolate import interp1d
fx = interp1d(np.arange(0,x.shape[0]), x.flatten())
fy = interp1d(np.arange(0,y.shape[1]), y.flatten())
for contour in contours:
contour[:,0] = fx(contour[:,0])
contour[:,1] = fy(contour[:,1])
查看结果和验证的简单代码:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
for contour in contours:
ax.plot(contour[:,0], contour[:,1])
fig.show()
Figure of the extracted contours