Animated plots exported as gifs? #49
Replies: 2 comments 2 replies
-
Maybe this is already pretty easy - in which case an example showing how to animate something would be awesome! |
Beta Was this translation helpful? Give feedback.
-
There have been numerous examples of animated plots. Here are a few more, but I recommend also searching the discussion posts for others
In most of these examples, you'll see a common pattern:
Easy enough! Though recently, a lot of work has gone into making animations easier and more streamlined with the addition of algorithm support in plotting (an advanced VTK+PyVista feature). Below, I'm going to share two common approaches I take for creating animations of time series data where the mesh structure typically remains constant but the color-mapped field changes through time Simple: update mesh in placeimport numpy as np
import pyvista as pv
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)
grid = pv.StructuredGrid(x, y, z)
grid['Height'] = z.ravel()
nframe = 15
time_space = np.linspace(0, 2 * np.pi, nframe + 1)
subsampled_time_space = time_space[:nframe]
pl = pv.Plotter()
pl.add_mesh(
grid,
scalars="Height",
lighting=False,
show_edges=True,
clim=[-1, 1],
)
pl.open_gif('wiggle.gif')
for t in subsampled_time_space:
z = np.sin(r + t)
grid.points[:, -1] = z.ravel() # modifies points
grid['Height'] = z.ravel() # modiefies colormapped scalars
pl.write_frame()
pl.close() Advanced: develop a custom algorithmimport numpy as np
import pyvista as pv
from vtk.util.vtkAlgorithm import VTKPythonAlgorithmBase
import xarray as xr
class TimeSeries(VTKPythonAlgorithmBase):
def __init__(self, mesh, data_array, name):
VTKPythonAlgorithmBase.__init__(
self, nInputPorts=0, nOutputPorts=1, outputType=mesh.GetClassName(),
)
self.mesh = mesh
self.data_array = data_array
self.name = name
self._time_step = 0
@property
def time_step(self):
return self._time_step
@time_step.setter
def time_step(self, t):
self._time_step = t
self.Modified() # critical
def RequestData(self, request, inInfo, outInfo):
"""Perform algorithm execution."""
try:
mesh = self.mesh.copy()
mesh[self.name] = self.data_array[self.time_step]
out = self.GetOutputData(outInfo, 0)
out.ShallowCopy(mesh)
except Exception as e: # pragma: no cover
traceback.print_exc()
raise e
return 1
mesh = pv.Sphere()
nt = 50
time_data = np.random.rand(nt, mesh.n_cells)
alg = TimeSeries(mesh, time_data, 'Field')
pl = pv.Plotter()
pl.add_mesh(
alg,
show_edges=True,
)
pl.open_gif('random.gif')
for t in range(nt):
alg.time_step = t
pl.write_frame()
pl.close() PyVista-Xarray has an algorithm already!Check out https://github.com/pyvista/pyvista-xarray/blob/main/pvxarray/vtk_source.py Here's an example of how to open a time series dataset with xarray and leverage this algorithm with PyVista: import pyvista as pv
from pvxarray import PyVistaXarraySource
import xarray as xr
ds = xr.tutorial.load_dataset("air_temperature")
da = ds.air # Select DataArray
nt = len(da.time)
alg = PyVistaXarraySource(da, x="lon", y="lat", time="time")
pl = pv.Plotter()
pl.add_mesh(alg)
pl.view_xy()
pl.open_gif("air_temperature.gif")
for tstep in range(nt):
alg.time_index = tstep
pl.write_frame()
pl.close() Note that this has 200+ timesteps and creates a 200MB gif, so I used MP4 to make shareable on web air_temperature.mp4Other examplesTake inspiration from the custom algorithms developed in these code bases Kale@brendanjmeade and I have been working with time series simulations and have made this module to abstract the data loading and "algorithm" Screen.Recording.2023-01-25.at.2.22.25.PM.movPan3DPan3D uses PyVista-Xarray and GeoVista to create interactive, cartographic, animated plots. This supports time series data and also demonstrates how PyVista-Xarray can load data at varying resolutions Screen.Recording.2022-09-20.at.12.15.06.PM.mov |
Beta Was this translation helpful? Give feedback.
-
A gif is worth 1000 plots - can we make them via pyvista-xarray?
I saw this pyvista example and immediately thought about plotting xarray data that has a
'time'
dimension as an animated 3D plot over time. Can we just wrap similar code to this example inside the pyvista accessor?cc @jbusecke
Beta Was this translation helpful? Give feedback.
All reactions