使用 matplotlib triplot 仅绘制三角形的边

Plot only edges of triangles with matplotlib triplot

Delaunay 单纯形的 triplot returns 两个 line2D 对象、边和节点的列表:

tri=scipy.spatial.Delaunay(points)
plt.triplot(points[:,0],points[:,1],tri.simplices.copy(),'k-o', label='Delaunay\ntriangulation')

如何在没有标记三角形节点(仅边缘)的情况下绘制 Delaunay 三角剖分? 或者,我想从图例中删除标记条目(将 'k-0' 替换为 'k-' 仍然会在图例中产生两个条目)。

plt.triplot 生成两个图例条目。第一个是边,第二个包含点(节点)。即使标记设置为 marker=None,此图例条目也会存在。

摆脱图例条目的最简单方法是获取图例句柄 (ax.get_legend_handles_labels()) 并仅使用其中的第一个创建图例。

h, l = plt.gca().get_legend_handles_labels()
plt.legend(handles=[h[0]],labels=[l[0]])

此时用户可以选择是否标记节点("k-o")或不标记("k-");将只有一个图例条目。

import numpy as np; np.random.seed(6)
import scipy.spatial
import matplotlib.pyplot as plt

points=np.random.rand(7, 2)

tri=scipy.spatial.Delaunay(points)
plt.triplot(points[:,0],points[:,1],tri.simplices.copy(),'k-o',
            label='Delaunay\ntriangulation')

h, l = plt.gca().get_legend_handles_labels()
plt.legend(handles=[h[0]],labels=[l[0]])
plt.show()