How to draw a Tetrahedron mesh by matplotlib?

Question:

I want to plot a tetrahedron mesh by matplotlib, and the following are a simple tetrahedron mesh:

xyz = np.array([
    [-1,-1,-1],
    [ 1,-1,-1], 
    [ 1, 1,-1],
    [-1, 1,-1],
    [-1,-1, 1],
    [ 1,-1, 1], 
    [ 1, 1, 1],
    [-1, 1, 1]], dtype=np.float) 

tets = np.array([
    [0,1,2,6],
    [0,5,1,6],
    [0,4,5,6],
    [0,7,4,6],
    [0,3,7,6],
    [0,2,3,6]], dtype=np.int)

Of course, in practical applications, the number of tetrahedrons in a mesh can be large. I can’t find any useful help information in google. So what is the better way to plot a tetrahedron mesh by matplotlib?

Furthermore, I can get all the triangle faces of the mesh.

tri = np.array([
    [0, 2, 1],
    [0, 1, 5],
    [0, 6, 1],
    [0, 3, 2],
    [0, 2, 6],
    [0, 6, 3],
    [0, 7, 3],
    [0, 5, 4],
    [0, 6, 4],
    [0, 4, 7],
    [0, 6, 5],
    [0, 6, 7],
    [1, 2, 6],
    [5, 1, 6],
    [2, 3, 6],
    [3, 7, 6],
    [4, 5, 6],
    [7, 4, 6]], dtype=np.int)
Asked By: Huayi Wei

||

Answers:

One can use mpl_toolkits.mplot3d.art3d.Poly3DCollection:

import mpl_toolkits.mplot3d as a3
axes = a3.Axes3D(pl.figure())
vts = xyz[tri, :]
tri = a3.art3d.Poly3DCollection(vts)
tri.set_alpha(0.2)
tri.set_color('grey')
axes.add_collection3d(tri)
axes.plot(point[:,0], point[:,1], point[:,2], 'ko')
axes.set_axis_off()
axes.set_aspect('equal')
pl.show()
Answered By: Huayi Wei

matplotlib is perhaps the wrong tool for the task. 3D plotting is hard, and there is, e.g., ParaView to try out. You can use meshio (a project of mine) to write your mesh to an appropriate data type:

import meshio
meshio.write('out.vtu', xyz, {'tetra': tets})

enter image description here

Answered By: Nico Schlömer
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.