Animating plots

How to make animated plots.

If multiple timesteps are loaded, then it is possible to make an animation with any of the plotting methods associated with a Variable object.

First, load the required modules.

from psipy.data import sample_data
from psipy.model import MASOutput

import astropy.constants as const
import astropy.units as u
import matplotlib.pyplot as plt

Next, load a set of MAS output files. Here we just download two succesive Carrington rotations, but you can change this line to point to any directory that has data files spanning multiple timesteps of a simulation.

mas_path = sample_data.mas_helio_timesteps()
model = MASOutput(mas_path)

Out:

Files Downloaded:   0%|          | 0/2 [00:00<?, ?file/s]

vr001.hdf:   0%|          | 0.00/7.96M [00:00<?, ?B/s]


vr002.hdf:   0%|          | 0.00/7.96M [00:00<?, ?B/s]

vr001.hdf:   0%|          | 2.00/7.96M [00:00<141:39:13, 15.6B/s]


vr002.hdf:   0%|          | 100/7.96M [00:00<2:48:04, 789B/s]


vr002.hdf:   1%|          | 42.0k/7.96M [00:00<00:39, 201kB/s]

vr001.hdf:   0%|          | 31.9k/7.96M [00:00<00:52, 150kB/s]


vr002.hdf:   3%|2         | 210k/7.96M [00:00<00:09, 801kB/s]

vr001.hdf:   2%|1         | 158k/7.96M [00:00<00:13, 591kB/s]


vr002.hdf:   6%|5         | 446k/7.96M [00:00<00:05, 1.34MB/s]

vr001.hdf:   5%|5         | 413k/7.96M [00:00<00:05, 1.28MB/s]


vr002.hdf:  14%|#3        | 1.08M/7.96M [00:00<00:02, 3.02MB/s]

vr001.hdf:  11%|#         | 856k/7.96M [00:00<00:03, 2.21MB/s]


vr002.hdf:  21%|##1       | 1.69M/7.96M [00:00<00:01, 4.02MB/s]

vr001.hdf:  18%|#7        | 1.40M/7.96M [00:00<00:02, 3.19MB/s]


vr002.hdf:  29%|##8       | 2.27M/7.96M [00:00<00:01, 4.34MB/s]

vr001.hdf:  28%|##7       | 2.22M/7.96M [00:00<00:01, 4.03MB/s]


vr002.hdf:  40%|####      | 3.19M/7.96M [00:00<00:00, 5.75MB/s]

vr001.hdf:  34%|###3      | 2.69M/7.96M [00:00<00:01, 4.14MB/s]

vr001.hdf:  39%|###8      | 3.09M/7.96M [00:01<00:01, 3.67MB/s]


vr002.hdf:  47%|####7     | 3.78M/7.96M [00:01<00:01, 3.61MB/s]

vr001.hdf:  43%|####3     | 3.46M/7.96M [00:01<00:01, 2.83MB/s]


vr002.hdf:  54%|#####4    | 4.30M/7.96M [00:01<00:00, 3.74MB/s]


vr002.hdf:  60%|#####9    | 4.75M/7.96M [00:01<00:00, 3.55MB/s]


vr002.hdf:  65%|######4   | 5.17M/7.96M [00:01<00:00, 3.69MB/s]

vr001.hdf:  47%|####7     | 3.77M/7.96M [00:01<00:02, 2.09MB/s]


vr002.hdf:  70%|#######   | 5.58M/7.96M [00:01<00:00, 3.50MB/s]

vr001.hdf:  50%|#####     | 4.02M/7.96M [00:01<00:02, 1.85MB/s]


vr002.hdf:  76%|#######5  | 6.02M/7.96M [00:01<00:00, 3.72MB/s]

vr001.hdf:  53%|#####3    | 4.25M/7.96M [00:01<00:01, 1.91MB/s]

vr001.hdf:  63%|######2   | 5.00M/7.96M [00:01<00:00, 3.06MB/s]

vr001.hdf:  78%|#######7  | 6.18M/7.96M [00:02<00:00, 5.02MB/s]

vr001.hdf:  88%|########7 | 6.97M/7.96M [00:02<00:00, 5.14MB/s]


vr002.hdf:  81%|########  | 6.42M/7.96M [00:02<00:01, 1.16MB/s]


vr002.hdf:  84%|########4 | 6.71M/7.96M [00:03<00:01, 1.16MB/s]


vr002.hdf:  94%|#########3| 7.47M/7.96M [00:03<00:00, 1.88MB/s]



Files Downloaded:  50%|#####     | 1/2 [00:03<00:03,  3.32s/file]

vr001.hdf:  95%|#########4| 7.55M/7.96M [00:03<00:00, 1.22MB/s]


Files Downloaded: 100%|##########| 2/2 [00:04<00:00,  1.83s/file]
Files Downloaded: 100%|##########| 2/2 [00:04<00:00,  2.05s/file]

Get a variable from the model output. We can also inspect the variable to see how many timesteps it has.

vr = model['vr']
print(f'Number of timesteps: {vr.n_timesteps}')

Out:

Number of timesteps: 2

We can now create an animation. In this example we plot a radial cut at the outer boundary of the model (i.e. a radial index of -1).

animation = vr.plot_radial_cut(r_idx=-1)

Note that animation is a matplotlib.animation.Animation object. To save the animation to disk you can use the save() method, e.g.: animation.save('mymovie.mp4')

Total running time of the script: ( 0 minutes 6.166 seconds)

Gallery generated by Sphinx-Gallery