Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (i.e. 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!

like image 852
Bryson of Heraclea Avatar asked Dec 09 '25 05:12

Bryson of Heraclea


1 Answers

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,
    )
like image 60
Nico Schlömer Avatar answered Dec 10 '25 17:12

Nico Schlömer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!