Debian Codegraph @

examples/02-plot/movie_glyphs.py

raw
1 """
2 .. _movie_glyphs_example:
3
4 Save a Movie Using Glyphs
5 ~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 Create an animated GIF by generating glyphs using :func:`glyph()
8 <pyvista.DataSetFilters.glyph>` using :func:`pyvista.Sphere`.
9
10 """
11
12 # sphinx_gallery_thumbnail_number = 1
13 import numpy as np
14 import pyvista as pv
15
16 # %%
17 # Create sphere glyphs
18 # ~~~~~~~~~~~~~~~~~~~~
19
20 x = np.arange(-10, 10, 1, dtype=float)
21 y = np.arange(-10, 10, 1, dtype=float)
22 x, y = np.meshgrid(x, y)
23 r = np.sqrt(x**2 + y**2)
24 z = (np.sin(r) + 1) / 2
25
26 # Create and structured surface
27 grid = pv.StructuredGrid(x, y, z)
28 grid.point_data['size'] = z.ravel()
29
30 # generate glyphs with varying size
31 sphere = pv.Sphere()
32 spheres = grid.glyph(scale='size', geom=sphere, orient=False)
33
34 spheres.plot(show_scalar_bar=False)
35
36 # %%
37 # Create the movie
38 # ~~~~~~~~~~~~~~~~
39
40 # Create a plotter object and set the scalars to the Z height
41 pl = pv.Plotter(notebook=False)
42 pl.add_mesh(
43 spheres,
44 show_edges=False,
45 show_scalar_bar=False,
46 clim=[0, 1],
47 cmap='bwr',
48 )
49
50 # Open a gif
51 pl.open_gif('glyph_wave.gif')
52
53 # Update Z and write a frame for each updated mesh
54 nframe = 20
55 for phase in np.linspace(0, 2 * np.pi, nframe + 1)[:nframe]:
56 z = (np.sin(r + phase) + 1) / 2
57
58 # regenerate spheres
59 grid = pv.StructuredGrid(x, y, z)
60 grid.point_data['size'] = z.ravel()
61 new_spheres = grid.glyph(scale='size', geom=sphere, orient=False)
62
63 spheres.copy_from(new_spheres)
64
65 # Write a frame. This triggers a render.
66 pl.write_frame()
67
68 # Close and finalize the gif
69 pl.close()
70 # %%
71 # .. tags:: plot