pyvista中常用的函数和应用--1
- 1. 多个图,类似于matplotlib
- 2. 指定颜色和平滑阴影
- 3. 创建点云
- 3.1 给点云赋值
- 3.2 画向量,把向量的值存在点云中
- 4. 画多面的图,并不是封闭
- 5. 画线条
- 5.1 让线条更加平滑
- 5.2 画中心线/细线
- 6. 绘制表明,曲面,三维mask
- 6.1 绘制平均曲率
- 7 创建一个空的grid结构,然后设置坐标
1. 多个图,类似于matplotlib
1 2 | p = pv.Plotter(shape=(3, 3)) p.subplot(0, 0) |
2. 指定颜色和平滑阴影
1 | supertoroid.plot(color="tan", smooth_shading=True) |
3. 创建点云
1 2 3 4 5 6 7 8 | import pyvista as pv import numpy as np ## 生成一组点云的坐标,然后构建点云的mesh points = np.random.rand(30000, 3) point_cloud = pv.PolyData(points) print np.allclose(points, point_cloud.points)#检测是否一致 # 画点云 point_cloud.plot(eye_dome_lighting=True) |
3.1 给点云赋值
1 2 3 4 | ## 给点云赋值,这里就把z轴坐标的值赋值给点云 data = points[:,-1] point_cloud["value"] = data point_cloud.plot(render_points_as_spheres=True) |
3.2 画向量,把向量的值存在点云中
因为mesh个一给点赋很多的值,通过字典的形式,只需要个数对的上,还有就是mesh中点的顺序要和值的存储顺序一样。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | def compute_vectors(mesh): origin = mesh.center vectors = mesh.points - origin vectors = vectors / np.linalg.norm(vectors, axis=1)[:, None] return vectors vectors = compute_vectors(point_cloud) point_cloud['vectors'] = vectors arrows = point_cloud.glyph(orient='vectors', scale=False, factor=0.15,) #通过这个函数构建箭头 # Display the arrows plotter = pv.Plotter() plotter.add_mesh(point_cloud, color='maroon', point_size=10., render_points_as_spheres=True) plotter.add_mesh(arrows, color='lightblue') # plotter.add_point_labels([point_cloud.center,], ['Center',], # point_color='yellow', point_size=20) plotter.show_grid() plotter.show() |
4. 画多面的图,并不是封闭
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import pyvista as pv # mesh points vertices = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0.5, 0.5, -1]]) # mesh faces,这个还不知道是什么意思,怎么定义? faces = np.hstack([[4, 0, 1, 2, 3], # square [3, 0, 1, 4], # triangle [3, 1, 2, 4]]) # triangle surf = pv.PolyData(vertices, faces) surf.cell_arrays['scalars'] = np.arange(3) # plot each face with a different color # surf.plot(scalars=np.arange(3), cpos=[-1, 1, 0.5]) p = pv.Plotter() ## 建一个普通画板 # p = pv.BackgroundPlotter() ## 建一个交互式画板 p.camera_position = [-1, 1, 0.5] p.add_mesh(surf) p.show() |
5. 画线条
https://docs.pyvista.org/examples/00-load/create-spline.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | def make_points(): """Helper to make XYZ points""" theta = np.linspace(-4 * np.pi, 4 * np.pi, 100) z = np.linspace(-2, 2, 100) r = z**2 + 1 x = r * np.sin(theta) y = r * np.cos(theta) return np.column_stack((x, y, z)) points = make_points() def polyline_from_points(points): poly = pv.PolyData() poly.points = points the_cell = np.arange(0, len(points), dtype=np.int) the_cell = np.insert(the_cell, 0, len(points)) poly.lines = the_cell return poly polyline = polyline_from_points(points) polyline["scalars"] = np.arange(polyline.n_points) tube = polyline.tube(radius=0.1) tube.plot(smooth_shading=True) |
5.1 让线条更加平滑
通过插值
1 2 3 4 5 6 7 | # Create spline with 1000 interpolation points spline = pv.Spline(points, 1000) # add scalars to spline and plot it spline["scalars"] = np.arange(spline.n_points) tube = spline.tube(radius=0.1) tube.plot(smooth_shading=True) |
5.2 画中心线/细线
样条线也可以绘制为普通线
1 2 3 4 5 | # generate same spline with 400 interpolation points spline = pv.Spline(points, 400) # plot without scalars spline.plot(line_width=4, color="k") |
6. 绘制表明,曲面,三维mask
https://docs.pyvista.org/examples/00-load/create-structured-surface.html
首先构建一个numpy的meshgrid,然后用pyvista画。
1 2 3 4 5 6 7 8 9 10 11 12 | import pyvista as pv from pyvista import examples import numpy as np x = np.arange(-10, 10, 0.25) y = np.arange(-10, 10, 0.25) x, y = np.meshgrid(x, y) r = np.sqrt(x ** 2 + y ** 2) z = np.sin(r) grid = pv.StructuredGrid(x, y, z) grid.plot() |
访问其中的点的值:
1 | grid.points |
6.1 绘制平均曲率
1 2 | # Plot mean curvature as well grid.plot_curvature(clim=[-1, 1]) |
7 创建一个空的grid结构,然后设置坐标
坐标的顺序是对的才可以,不然画出来会是乱的。
1 2 3 4 5 6 7 8 9 10 | points = np.random.rand(30000, 3) mesh = pv.StructuredGrid() # Set the coordinates from the numpy array mesh.points = points # set the dimensions mesh.dimensions = [29, 32, 1] # and then inspect it! mesh.plot(show_edges=True, show_grid=True, cpos="xy") |
下图就是乱的,因为坐标是随机生成的,坐标的顺序是混乱的。