【问题标题】:Plot only edges of triangles with matplotlib triplot使用 matplotlib triplot 仅绘制三角形的边
【发布时间】:2017-08-05 01:49:54
【问题描述】:

Delaunay 单纯形的triplot 返回一个包含两个 line2D 对象、边和节点的列表:

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

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

【问题讨论】:

    标签: python-3.x matplotlib delaunay


    【解决方案1】:

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

    【讨论】:

    • 谢谢!这确实是最简单的方法。现在我知道如何控制图例条目了。
    猜你喜欢
    • 1970-01-01
    • 2015-03-30
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 2020-01-13
    • 2013-11-30
    相关资源
    最近更新 更多