如何在python中生成网格并绘制3d曲面?

时间:2019-12-18 12:10:01

标签: python matplotlib pyvista

我有3d曲面的6个角点坐标,如图1所示。我想生成并绘制3d曲面,如图2所示。我需要找到每个网格化区域的中点到原点的距离。
Figure 1

Figure 2

请建议我,哪个模块更适合网格划分和绘制?

3 个答案:

答案 0 :(得分:0)

我不知道您的数据是什么样子,但是matplotlib能够基于具有x,y和z坐标的点绘制3D曲面。

请参阅:https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html

答案 1 :(得分:0)

您可以使用vtkplotter

from vtkplotter import *

pts = [(-5.795555, -4, 1.55291), (-4.829629, -2, 1.294095),
       (-5.795555, 1, 1.552914), (-5.536736, -4, 2.51884),
       (-4.57081, -2, 2.260021), (-5.536736,  1, 2.51884)]

faces = [(0,3,4,1), (1,4,5,2)]

mesh = Actor([pts, faces]).color('red').alpha(0.3).lineWidth(2)
labels = [Text(i, pos=pts[i], s=.2, c='white') for i in range(len(pts))]

show(mesh, labels)

script result

答案 2 :(得分:0)

PyVistavtkplotter非常相似,两者都是基于VTK构建的,但API /设计选择却大不相同。供您使用,两者都很棒!为了完整起见,这与PyVista等效。

通常使用类似这样的简单几何体,Matplotlib,PyVista或vtkplotter都可以很好地完成。如果您开始创建更复杂的网格物体和3D场景,那么PyVista和vtkplotter就是为3D而建的,而MPL在2D上确实很棒,因此它们将是出色的。

如果您开始制作复杂的网格.... PyVista将特别擅长数据管理....无耻的插件;)

import pyvista as pv
import numpy as np

# Define the nodes
pts = np.array([(-5.795555, -4, 1.55291), (-4.829629, -2, 1.294095),
       (-5.795555, 1, 1.552914), (-5.536736, -4, 2.51884),
       (-4.57081, -2, 2.260021), (-5.536736,  1, 2.51884)])
# Define the quads
faces = np.array([(4,0,3,4,1), (4,1,4,5,2)])

# Instantiate a mesh
mesh = pv.PolyData(pts, faces)

# Create a plotting window and display!
p = pv.Plotter()

# Add the mesh and some labels
p.add_mesh(mesh, show_edges=True)
p.add_point_labels(mesh.points, ["%d"%i for i in range(mesh.n_points)])

# A pretty view position
p.camera_position = [(-11.352247399703748, -3.421477319390501, 9.827830270231935),
 (-5.1831825, -1.5, 1.9064675),
 (-0.48313206526616853, 0.8593146723923926, -0.16781448484204659)]

# Render it!
p.show()

enter image description here