1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
"""
.. _gif_example:
Create a GIF Movie
~~~~~~~~~~~~~~~~~~
Generate a moving gif from an active plotter.
This example uses :meth:`~pyvista.Plotter.open_gif` and
:meth:`~pyvista.Plotter.write_frame` to create the gif.
.. note::
Use ``lighting=False`` to reduce the size of the color space to avoid
"jittery" GIFs, especially for the scalar bar.
"""
# sphinx_gallery_thumbnail_number = 2
from __future__ import annotations
import numpy as np
import pyvista as pv
# %%
# Create a structured grid
# ~~~~~~~~~~~~~~~~~~~~~~~~
# Create a structured grid and make a "wave" my shifting the Z position based
# on the cartesian distance from the origin.
x = np.arange(-10, 10, 0.5)
y = np.arange(-10, 10, 0.5)
x, y = np.meshgrid(x, y)
r = np.sqrt(x**2 + y**2)
z = np.sin(r)
# Create and structured surface
grid = pv.StructuredGrid(x, y, z)
grid['Height'] = z.ravel()
grid.plot()
# %%
# Generate a GIF
# ~~~~~~~~~~~~~~
# Generate a GIF using ``off_screen=True`` parameter.
# Create a plotter object and set the scalars to the Z height
plotter = pv.Plotter(notebook=False, off_screen=True)
plotter.add_mesh(
grid,
scalars='Height',
lighting=False,
show_edges=True,
clim=[-1, 1],
)
# Open a gif
plotter.open_gif('wave.gif')
# Update Z and write a frame for each updated position
nframe = 15
for phase in np.linspace(0, 2 * np.pi, nframe + 1)[:nframe]:
z = np.sin(r + phase)
# Update values inplace
grid.points[:, -1] = z.ravel()
grid['Height'] = z.ravel()
# Write a frame. This triggers a render.
plotter.write_frame()
# Closes and finalizes movie
plotter.close()
# %%
# .. tags:: plot
|