Celluloid animation package and Pygmt figures (figure axes)

Good afternoon!

I have a problem trying to capture an animation using a pygmt figure object and the Celluloid package (celluloid package website). When I try to put the normal celluloid code around a pygmt figure, it has trouble finding the figure axes:

---> 563     camera.snap()
 564 

~/anaconda3/envs/obspy/lib/python3.7/site-packages/celluloid.py in snap(self)
 28         """Capture current state of the figure."""
 29         frame_artists: List[Artist] = []
---> 30         for i, axis in enumerate(self._figure.axes):
 31             if axis.legend_ is not None:
 32                 axis.add_artist(axis.legend_)

AttributeError: 'Figure' object has no attribute 'axes'

My figure does work normally without the celluloid package and I just initialize it in a normal way using:

fig = pygmt.Figure()

Does anyone have experience with how to extract the axes or does anyone have another easy way to animate a pygmt figure in a loop?

Thanks in advance!

Hi @ktuin, thanks for trying out PyGMT! To answer you main question on creating an animation, you would want to use GMT’s movie function. Unfortunately that isn’t available for Python/PyGMT yet (although there’s been some talk about adding it). The celluloid package you linked above does look great, but it seems to be designed for matplotlib plots.

There are a few options you can try:

  1. If you’re using Jupyter Lab/Notebook, you can try to access pure GMT commands like movie using an exclamation mark, e.g.
!gmt movie ...
  1. Assuming that you’re on Linux/macOS, you can try producing the individual PNG image files using convert on bash. See e.g. Animation weather system (gif) and https://unix.stackexchange.com/a/24019
convert -delay 120 -loop 0 fig_*.png animation.gif

Hopefully that helps. If you have time, feel free to open a feature request for movie on GitHub, and someone might start working on it.

Hi @weiji14, thanks so much for your reply! Yes I indeed saw that celluloid doesn’t really work for images other than matplotlib. Thanks very much though for the movie function, I will check it out!

1 Like