How to assign variable fig for animation function with yt.create_scene

I want to make an animation of multiple plots whose rendering evolves in time. The files that I need are under the format, for example for one : `DD0043/DD0043`. So I use the trick : `f'{43:04}'` to fill the zeros leading for each file (the files go from `DD0000/DD0000` to `DD0922/DD0922`. Here the script, warning, the plot is done with `yt-project` tool : import yt import os, sys import numpy as np from matplotlib.animation import FuncAnimation from matplotlib import rc_context from matplotlib import pyplot as plt # animate must accept an integer frame number. We use the frame number # to identify which dataset in the time series we want to load def animate(i): plot._switch_ds(array_data[i]) # Number of files numFiles = os.system('ls -dl DD* | wc -l') # Array for each data directory array_data = np.array(numFiles) for i in range(numFiles): data = yt.load('DD'+str(f'{i:04}')+'/DD'+str(f'{i:04}')) sc = yt.create_scene(data, lens_type='perspective') source = sc[0] source.set_field('density') source.set_log(True) # Set up the camera parameters: focus, width, resolution, and image orientation sc.camera.focus = ds.domain_center sc.camera.resolution = 1024 sc.camera.north_vector = [0, 0, 1] sc.camera.position = [1.7, 1.7, 1.7] # You may need to adjust the alpha values to get an image with good contrast. # For the annotate_domain call, the fourth value in the color tuple is the # alpha value. sc.annotate_axes(alpha=.02) sc.annotate_domain(ds, color=[1, 1, 1, .01]) text_string = "T = {} Gyr".format(float(array_data[i].current_time.to ('Gyr'))) fig = plt.figure() animation = FuncAnimation(fig, animate, frames=numFiles) # Override matplotlib's defaults to get a nicer looking font with rc_context({'mathtext.fontset': 'stix'}): animation.save('animation.mp4') But at the execution, I get the following error : 923 Traceback (most recent call last): File "vol-annotated.py", line 52, in <module> animation.save('animation.mp4') File "/Users/fab/Library/Python/3.7/lib/python/site-packages/matplotlib/animation.py", line 1135, in save anim._init_draw() File "/Users/fab/Library/Python/3.7/lib/python/site-packages/matplotlib/animation.py", line 1743, in _init_draw self._draw_frame(next(self.new_frame_seq())) StopIteration I don't know if I do the things correctly, especially for the variable `fig` that I initialize with : fig = plt.figure() Actually, I am trying to adapt to my case this script which creates a movie : i.e : import yt from matplotlib.animation import FuncAnimation from matplotlib import rc_context ts = yt.load('GasSloshingLowRes/sloshing_low_res_hdf5_plt_cnt_*') plot = yt.SlicePlot(ts[0], 'z', 'density') plot.set_zlim('density', 8e-29, 3e-26) fig = plot.plots['density'].figure # animate must accept an integer frame number. We use the frame number # to identify which dataset in the time series we want to load def animate(i): ds = ts[i] plot._switch_ds(ds) animation = FuncAnimation(fig, animate, frames=len(ts)) # Override matplotlib's defaults to get a nicer looking font with rc_context({'mathtext.fontset': 'stix'}): animation.save('animation.mp4') Any help is welcome, Regards [1]: https://yt-project.org/doc/cookbook/complex_plots.html#time-series-movie
participants (1)
-
henri petit