简体   繁体   中英

How to plot an interpolated scalar field on a 3D triangulated surface in Python

My problem is as follows: I have a triangulated surface with arrays X,Y,Z containing the coordinates of the mesh vertices and array Triangles containing the indices of the vertex points for each corner of the triangle (ie each line of the array contains a triplet defining a triangle in terms of vertices). Furthermore, I have an array Field containing scalar field values on each vertex of the mesh. I wish to plot the triangular mesh in Python, where each triangle is colored according to the field values of its vertices.

I have found a solution for this problem, if one assigns a single color per triangle (see here ). What if I want to interpolate the field values over the surface of each single triangle, so that there is a continuous (smooth even better) coloring of the mesh?

I thought to use the tri_api of Matplotlib, because it can also do interpolation over triangular grids, however I have not found a solution that suits my needs. This demo comes close, but it is restricted on flat surfaces and interpolates on a rectangular grid.

Of course, the solution does not have to entail Matplotlib, it can be any other Python library/toolbox. I would be grateful for any suggestions!

Plotting in 3D is hard, and there are other software tools that you can use, notably ParaView . You'll first have to convert your mesh into a file that ParaView can use, though. To this end, you can for example use meshio (which I wrote):

import numpy
import meshio

points = numpy.array([
    [0.0, 0.0, 0.0],
    [0.0, 1.0, 0.0],
    [0.0, 0.0, 1.0],
    ])
cells = {
    "triangle": numpy.array([
        [0, 1, 2]
        ])
    }
meshio.write_points_cells(
    "foo.vtk",
    points,
    cells,
    point_data=point_data,
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM