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