我有一个散点图和许多等高线图。有很多轮廓,有些则关闭。如何从[this]中识别最大闭合轮廓的值。1
答案 0 :(得分:3)
找到最大的闭合轮廓可以如下完成;请注意,这假设是由最大的"你的意思是最大的点对点距离。
其他尺寸指标可以轻松替代。
另请注意,如果您使用extend3d=True
版本,则需要操纵创建的Poly3DCollection
,这有点棘手。
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()
这导致: