Debian Codegraph @

examples/01-filter/glyph.py

raw
1 """
2 .. _glyph_example:
3
4 Plotting Glyphs (Vectors or PolyData)
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 Use vectors in a dataset to plot and orient glyphs/geometric objects.
8 """
9
10 import math
11
12 import numpy as np
13
14 # sphinx_gallery_thumbnail_number = 4
15 import pyvista as pv
16 from pyvista import examples
17
18 # %%
19 # Glyphying can be done via the :func:`pyvista.DataSetFilters.glyph` filter
20
21 mesh = examples.download_carotid().threshold(145, scalars='scalars')
22 mask = mesh['scalars'] < 210
23 mesh['scalars'][mask] = 0 # null out smaller vectors
24
25 # Make a geometric object to use as the glyph
26 geom = pv.Arrow() # This could be any dataset
27
28 # Perform the glyph
29 glyphs = mesh.glyph(orient='vectors', scale='scalars', factor=0.003, geom=geom)
30
31 # plot using the plotting class
32 pl = pv.Plotter()
33 pl.add_mesh(glyphs, show_scalar_bar=False, lighting=False, cmap='coolwarm')
34 pl.camera_position = pv.CameraPosition(
35 position=(146.53, 91.28, 21.70),
36 focal_point=(125.00, 94.45, 19.81),
37 viewup=(-0.086, 0.007, 0.996),
38 ) # view only part of the vector field
39 cpos = pl.show(return_cpos=True)
40
41
42 # %%
43 # Another approach is to load the vectors directly to the mesh object and then
44 # access the :attr:`pyvista.DataSet.arrows` property.
45
46 sphere = pv.Sphere(radius=math.pi)
47
48 # make cool swirly pattern
49 vectors = np.vstack(
50 (
51 np.sin(sphere.points[:, 0]),
52 np.cos(sphere.points[:, 1]),
53 np.cos(sphere.points[:, 2]),
54 ),
55 ).T
56
57 # add and scale
58 sphere['vectors'] = vectors * 0.3
59 sphere.set_active_vectors('vectors')
60
61 # plot just the arrows
62 sphere.arrows.plot()
63
64 # %%
65 # Plot the arrows and the sphere.
66
67 pl = pv.Plotter()
68 pl.add_mesh(sphere.arrows, lighting=False, scalar_bar_args={'title': 'Vector Magnitude'})
69 pl.add_mesh(sphere, color='grey', ambient=0.6, opacity=0.5, show_edges=False)
70 pl.show()
71
72
73 # %%
74 # Subset of Glyphs
75 # ++++++++++++++++
76 #
77 # Sometimes you might not want glyphs for every node in the input dataset. In
78 # this case, you can choose to build glyphs for a subset of the input dataset
79 # by using a merging tolerance. Here we specify a merging tolerance of five
80 # percent which equates to five percent of the bounding box's length.
81
82 # Example dataset with normals
83 mesh = examples.load_random_hills()
84
85 # create a subset of arrows using the glyph filter
86 arrows = mesh.glyph(scale='Normals', orient='Normals', tolerance=0.05)
87
88 pl = pv.Plotter()
89 pl.add_mesh(arrows, color='black')
90 pl.add_mesh(mesh, scalars='Elevation', cmap='terrain', smooth_shading=True)
91 pl.show()
92 # %%
93 # .. tags:: filter