Search code examples
python-3.xmatplotlibdelaunay

Plot only edges of triangles with matplotlib triplot


A triplot of Delaunay simplices returns a list of two line2D objects, edges and nodes:

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

How can I to plot a Delaunay triangulation without markers for the triangle nodes, (only the edges)? Alternatively, I want to remove the marker entry from the legend (replacing 'k-0' with 'k-' still produces two entries in the legend). delaunay


Solution

  • The plt.triplot produces two legend entries. The first of those are the edges, the second contains the points (nodes). Even if the marker is set to marker=None, this legend entry will be present.

    The easiest way to get rid of the legend entry, is to obtain the legend handles (ax.get_legend_handles_labels()) and create a legend only using the first of those.

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

    At this point it's the user's choice whether to have the nodes marked ("k-o") or not ("k-"); there will only be one legend entry.

    enter image description here

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