目录
Pyvista可视化入门
网格创建
1. 非结构网格对象
"""this script is used to visualize the point cloud of the mesh
"""
import numpy as np
import pyvista as pv
from pyvista import CellType
# node data
# these points will all be shared between the cells
points = np.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.5, 0.0, 0.0],
[1.0, 1.0, 0.0],
[1.0, 0.5, 0.0],
[0.0, 1.0, 0.0],
[0.5, 1.0, 0.0],
[0.0, 0.5, 0.0],
[0.5, 0.5, 0.0],
[1.0, 0.0, 0.5],
[1.0, 0.0, 1.0],
[0.0, 0.0, 0.5],
[0.0, 0.0, 1.0],
[0.5, 0.0, 0.5],
[0.5, 0.0, 1.0],
[1.0, 1.0, 0.5],
[1.0, 1.0, 1.0],
[1.0, 0.5, 0.5],
[1.0, 0.5, 1.0],
[0.0, 1.0, 0.5],
[0.0, 1.0, 1.0],
[0.5, 1.0, 0.5],
[0.5, 1.0, 1.0],
[0.0, 0.5, 0.5],
[0.0, 0.5, 1.0],
[0.5, 0.5, 0.5],
[0.5, 0.5, 1.0],
],
)
# 定义单元的连通性,第一列是单元节点个数,后续是节点编号(0-based)
cells = np.array(
[
[8, 0, 2, 8, 7, 11, 13, 25, 23],
[8, 2, 1, 4, 8, 13, 9, 17, 25],
[8, 7, 8, 6, 5, 23, 25, 21, 19],
[8, 8, 4, 3, 6, 25, 17, 15, 21],
[8, 11, 13, 25, 23, 12, 14, 26, 24],
[8, 13, 9, 17, 25, 14, 10, 18, 26],
[8, 23, 25, 21, 19, 24, 26, 22, 20],
[8, 25, 17, 15, 21, 26, 18, 16, 22],
],dtype=int)
# 定义单元类型数组,每个单元类型对应一个整数
celltypes = CellType.HEXAHEDRON*np.ones(cells.shape[0],dtype=int)
# 初始化pyvista的UnstructuredGrid对象
grid = pv.UnstructuredGrid(cells, celltypes, points)
# 显示网格
grid.plot(show_edges=True, color='w', point_size=10)
2. 在网格中添加显示点对象
通过突出显示点对象可以,代表约束的节点
"""this script is used to visualize the point cloud of the mesh
"""
import numpy as np
import pyvista as pv
from pyvista import CellType
# node data
# these points will all be shared between the cells
points = np.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.5, 0.0, 0.0],
[1.0, 1.0, 0.0],
[1.0, 0.5, 0.0],
[0.0, 1.0, 0.0],
[0.5, 1.0, 0.0],
[0.0, 0.5, 0.0],
[0.5, 0.5, 0.0],
[1.0, 0.0, 0.5],
[1.0, 0.0, 1.0],
[0.0, 0.0, 0.5],
[0.0, 0.0, 1.0],
[0.5, 0.0, 0.5],
[0.5, 0.0, 1.0],
[1.0, 1.0, 0.5],
[1.0, 1.0, 1.0],
[1.0, 0.5, 0.5],
[1.0, 0.5, 1.0],
[0.0, 1.0, 0.5],
[0.0, 1.0, 1.0],
[0.5, 1.0, 0.5],
[0.5, 1.0, 1.0],
[0.0, 0.5, 0.5],
[0.0, 0.5, 1.0],
[0.5, 0.5, 0.5],
[0.5, 0.5, 1.0],
],
)
# 定义单元的连通性,第一列是单元节点个数,后续是节点编号(0-based)
cells = np.array(
[
[8, 0, 2, 8, 7, 11, 13, 25, 23],
[8, 2, 1, 4, 8, 13, 9, 17, 25],
[8, 7, 8, 6, 5, 23, 25, 21, 19],
[8, 8, 4, 3, 6, 25, 17, 15, 21],
[8, 11, 13, 25, 23, 12, 14, 26, 24],
[8, 13, 9, 17, 25, 14, 10, 18, 26],
[8, 23, 25, 21, 19, 24, 26, 22, 20],
[8, 25, 17, 15, 21, 26, 18, 16, 22],
],dtype=int)
# 定义单元类型数组,每个单元类型对应一个整数
celltypes = CellType.HEXAHEDRON*np.ones(cells.shape[0],dtype=int)
# 初始化pyvista的UnstructuredGrid对象
grid = pv.UnstructuredGrid(cells, celltypes, points)
# 显示网格
# grid.plot(show_edges=True, color='w', point_size=10)
point_cloud=pv.PolyData(points[:9,:])
p=pv.Plotter()
# 给plotter()添加网格和控制这个网格显示的参数
p.add_mesh(grid,show_edges=True, color='r',style='wireframe')
# 将点云加入绘图器,并且设置点的大小和颜色
p.add_mesh(point_cloud,color='b',point_size=15,render_points_as_spheres=True)
p.show()
3. 制作箭头并附加到Dataset的位置
import pyvista as pv
from pyvista import examples
mesh = examples.load_random_hills()
arrows = mesh.glyph(
scale="Normals", orient="Normals", tolerance=0.05
)
pl = pv.Plotter()
actor = pl.add_mesh(arrows, color="black")
actor = pl.add_mesh(
mesh,
scalars="Elevation",
cmap="terrain",
show_scalar_bar=False,
)
pl.show()

绘图选项
pyvista的绘图接口有两种方法:
- 使用数据对象的plot()方法
import numpy as np
import pyvista as pv
from pyvista import CellType
# node data
# these points will all be shared between the cells
points = np.array(
[
...
],
)
# 定义单元的连通性,第一列是单元节点个数,后续是节点编号(0-based)
cells = np.array(
[
.....
],dtype=int)
# 定义单元类型数组,每个单元类型对应一个整数
celltypes = CellType.HEXAHEDRON*np.ones(cells.shape[0],dtype=int)
# 初始化pyvista的UnstructuredGrid对象
grid = pv.UnstructuredGrid(cells, celltypes, points)
# 显示网格
grid.plot(show_edges=True, color='w', point_size=10)- 使用pyvista.Plotter()的show()方法
"""this script is used to visualize the point cloud of the mesh
"""
import numpy as np
import pyvista as pv
from pyvista import CellType
# node data
# these points will all be shared between the cells
points = np.array(
[
.....
],
)
# 定义单元的连通性,第一列是单元节点个数,后续是节点编号(0-based)
cells = np.array(
[
...
],dtype=int)
# 定义单元类型数组,每个单元类型对应一个整数
celltypes = CellType.HEXAHEDRON*np.ones(cells.shape[0],dtype=int)
# 初始化pyvista的UnstructuredGrid对象
grid = pv.UnstructuredGrid(cells, celltypes, points)
# 显示网格
p=pv.Plotter()
# 给plotter()添加网格和控制这个网格显示的参数
p.add_mesh(grid,show_edges=True, color='r', point_size=10,style='wireframe',render_points_as_spheres=False)
p.show()
点云
https://docs.pyvista.org/examples/02-plot/point-clouds.html
单元类型

