查找最大的封闭3D等高线图

用户名

我有一个散点图和许多轮廓图。轮廓很多,有些轮廓封闭,有些轮廓则没有。如何从[this]识别最大闭合轮廓的值。1个

梅特兹

找到最大的闭合轮廓可以按照以下步骤进行:请注意,尽管这假设“最大”是指最大的点对点距离。

不过,其他尺寸指标也可以轻松替换。

还要注意,如果使用该extend3d=True版本,则需要操纵Poly3DCollection创建的get,这会有些棘手。

from mpl_toolkits.mplot3d import axes3d, art3d
import matplotlib.pyplot as plt
from matplotlib import cm
from scipy.spatial import distance_matrix
import numpy as np    

fig = plt.figure()
ax = fig.gca(projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
cset = ax.contour(X, Y, Z, cmap=cm.coolwarm)
ax.clabel(cset, fontsize=9, inline=1)

maxsize = 0
# Iterate over all the contour segments and find the largest
for i, segs in enumerate(cset.allsegs):
    for j, seg in enumerate(segs):
        # First make sure it's closed
        if (seg[0]-seg[-1]).any():
            continue
        # Now get it's size
        size = distance_matrix(seg, seg).max()
        if size > maxsize:
            maxsize = size
            maxseg = (i, j)

# Now highlight the "biggest" closed contour
cset.collections[maxseg[0]].set_color('y')
cset.collections[maxseg[0]].set_lw(5)
pts2d = cset.allsegs[maxseg[0]][maxseg[1]]
z = cset.levels[maxseg[0]]
coords = np.c_[ pts2d, z*np.ones(pts2d.shape[0]) ]
print(coords)

plt.show()

结果是:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章