Search code examples
python-3.xmatplotlibmeshtriangulation

How to plot a triangular 3d mesh


In 2d it is quite simple to plot a triangular mesh given by the vertices and the triangulation

import matplotlib.pyplot as plt
import numpy as np
T = np.array([[0,1,2], [1,2,3]])
vertices = np.array([[0,0],[1,0],[0,1],[1,2]])
plt.triplot(vertices[:,0], vertices[:,1], T)
plt.show()

Now I'd like to do the exact same thing but in three dimension, that is with e.g.

vertices = np.array([[0,0,0],[1,0,0],[0,1,0],[1,2,1]])

Is there a simple way to achieve that in matplotlib? (At least simpler than looping over the triangles and plotting the edges manually?)


Solution

  • You can achieve it in a very similar way using mplot3d:

    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    import numpy as np
    
    vertices = np.array([[0,0,0],[1,0,0],[0,1,0],[1,2,1]])
    T = np.array([[0,1,2], [1,2,3]])
    
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.plot_trisurf(vertices[:,0], vertices[:,1], vertices[:,2], triangles = T, edgecolor=[[0,0,0]], linewidth=1.0, alpha=0.0, shade=False)
    
    plt.show()
    

    enter image description here