vtk python在四边形网格上可视化标量数据

计算科学 Python 可视化 VTK
2021-12-25 20:30:11

我想在 Python3 中使用 VTK 可视化位于四边形网格(2D)节点上的标量数据。我发现的最接近的例子是vtk quad mesh,但它似乎只可视化网格,而不是网格上的数据。有人可以指出一个更好的例子吗?

2个回答

正如@AloneProgrammer 在评论中提到的那样,您可以使用 ParaView,因为它很容易。正如@WolfgangBangerth 添加的那样,您还可以尝试为VTK 算法提供前端的VisIt 。

如果您需要通过 Python 脚本以编程方式执行此操作,您可以使用:

  • VTK本身。API 不是最直观的,但您可以习惯它。它使您可以控制每个细节。

  • PyVista提供了一个易于使用的 API。

  • Mayavi也很容易使用。

这是我想做的一个简单的工作示例,使用 PyVista。它需要 X11 来绘制。因此,如果您在没有 X11 的系统上运行(例如 WSL2 下的 Ubuntu),您可能需要安装 Xvfb。请参阅此处了解更多说明。

此示例基于此 PyVista 示例

import numpy as np
import pyvista as pv

# mesh points
vertices = np.array([[0, 0, 0],
                     [1, 0, 0],
                     [1, 1, 0],
                     [0, 1, 0],
                     [2, 0, 0],
                     [3, 1, 0]])

# mesh faces
# the 4 indicates how many nodes are present
# other numbers are indicies in the vertices array
faces = np.hstack([[4, 0, 1, 2, 3],
                   [4, 1, 4, 5, 2]])   

# define a surface or as PyVista calls it a 'mesh' object
surf = pv.PolyData(vertices, faces)
# define nodal/point data
surf.point_arrays['point1'] = np.arange(surf.n_points)
# define cell/element data
surf.cell_arrays['cell1']   = np.arange(surf.n_cells)

# parameters for the colorbar
sargs = dict(height=0.25, vertical=True, position_x=0.85, position_y=0.05)

# create the plotter
p = pv.Plotter()
# show the axes widget 
p.show_axes()
# add the mesh to the plotter and tell it which field to display
# and set the arguments for the scalar bar
p.add_mesh(surf,scalars='point1',scalar_bar_args=sargs)
# specify the view
p.view_xy()
# plot and take a screenshot
p.show(screenshot='pyvistatest.png')