Debian Codegraph @

examples/02-plot/vertices.py

raw
1 """
2 .. _vertices_example:
3
4 Visible Vertices
5 ~~~~~~~~~~~~~~~~
6
7 Display vertices on a mesh in the same fashion as edge visibility.
8 """
9
10 # sphinx_gallery_thumbnail_number = 3
11 import pyvista as pv
12 from pyvista import examples
13
14 mesh = examples.download_bunny_coarse()
15
16 cpos = pv.CameraPosition(
17 position=(0.036, 0.367, 0.884),
18 focal_point=(0.024, 0.033, -0.022),
19 viewup=(-0.303, 0.895, -0.325),
20 )
21
22 # %%
23 # We can easily display all vertices of a mesh with a ``points`` style
24 # representation when plotting:
25 pl = pv.Plotter()
26 pl.add_mesh(
27 mesh, style='points', color='magenta', render_points_as_spheres=True, point_size=10
28 )
29 pl.show(cpos=cpos)
30
31
32 # %%
33 # However, we often want to see the vertices of a mesh rendered atop the
34 # surface geometry. Much like how we can render the edges of a mesh:
35 pl = pv.Plotter()
36 pl.add_mesh(mesh, show_edges=True)
37 pl.show(cpos=cpos)
38
39
40 # %%
41 # In order to display the vertices atop a mesh's surface geometry, simply pass
42 # ``show_vertices=True`` to render them along side the original geometry.
43 #
44 # .. note::
45 # Vertex styling can be changed using ``vertex_color``, ``vertex_opacity``,
46 # and ``vertex_style``.
47 pl = pv.Plotter()
48 pl.add_mesh(
49 mesh,
50 show_edges=True,
51 vertex_color='magenta',
52 render_points_as_spheres=True,
53 point_size=10,
54 show_vertices=True,
55 )
56 pl.show(cpos=cpos)
57
58
59 # %%
60 # If you wish to have further control over the way surface points are plotted
61 # alongside the surface geometry, extract the surface points and plot them
62 # separately.
63 #
64 # The first step is to extract the outer surface geometry of the mesh then
65 # grab all the points of that extraction.
66 surf_points = mesh.extract_surface(algorithm=None).points
67
68
69 # %%
70 # Now that we have the vertices extracted, we can use :func:`add_points()
71 # <pyvista.Plotter.add_points>` to render them along side the original
72 # geometry.
73 #
74 # Color the points by their Y position.
75
76 pl = pv.Plotter()
77 pl.add_mesh(mesh, show_edges=True)
78 pl.add_points(
79 surf_points,
80 color='magenta',
81 render_points_as_spheres=True,
82 point_size=10,
83 scalars=surf_points[:, 1],
84 lighting=False,
85 show_scalar_bar=False,
86 )
87 pl.show(cpos=cpos)
88 # %%
89 # .. tags:: plot