我想在 Python3 中使用 VTK 可视化位于四边形网格(2D)节点上的标量数据。我发现的最接近的例子是vtk quad mesh,但它似乎只可视化网格,而不是网格上的数据。有人可以指出一个更好的例子吗?
vtk python在四边形网格上可视化标量数据
计算科学
Python
可视化
VTK
2021-12-25 20:30:11
2个回答
这是我想做的一个简单的工作示例,使用 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')
其它你可能感兴趣的问题