0

I have a Finite Difference Simulation that is plotted with the following command:

anim = ani.FuncAnimation(fig, update, init_func = init, frames = np.linspace(0, t_max, int(t_max / dt + 1)), repeat = False) #  repeat = False
plt.show()

This gives a constantly updating figure as expected. However, I would like to try and save this as literally any type of video format, i.e. .gif, .mp4, .mov, and none work.

anim.save("FDA Simulation.gif", writer = 'imagemagick', fps = 30)

The following error is generated:

jacobivanov@Jacob-Ivanovs-MacBook-Air 1D Heat Transfer % /opt/homebr
ew/bin/python3 "/Users/jacobivanov/Desktop/University of Connecticut
/Computational Fluid Dynamics Group/1D Heat Transfer/FDA_Simulation.
py"
MovieWriter stderr:
convert: unexpected end-of-file '-': No such file or directory @ error/rgb.c/ReadRGBImage/249.
convert: no images defined `FDA Simulation.gif' @ error/convert.c/ConvertImageCommand/3342.

Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/animation.py", line 231, in saving
    yield self
  File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/animation.py", line 1081, in save
    anim._draw_next_frame(d, blit=False)
  File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/animation.py", line 1116, in _draw_next_frame
    self._draw_frame(framedata)
  File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/animation.py", line 1743, in _draw_frame
    self._drawn_artists = self._func(framedata, *self._args)
  File "/Users/jacobivanov/Desktop/University of Connecticut/Computational Fluid Dynamics Group/1D Heat Transfer/FDA_Simulation.py", line 96, in update
    fig.show()
  File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/figure.py", line 2632, in show
    raise AttributeError(
AttributeError: Figure.show works only for figures managed by pyplot, normally created by pyplot.figure()

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jacobivanov/Desktop/University of Connecticut/Computational Fluid Dynamics Group/1D Heat Transfer/FDA_Simulation.py", line 112, in <module>
    anim.save("FDA Simulation.gif", writer = 'imagemagick', fps = 30)
  File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/animation.py", line 1063, in save
    with mpl.rc_context({'savefig.bbox': None}), \
  File "/opt/homebrew/Cellar/python@3.10/3.10.9/Frameworks/Python.framework/Versions/3.10/lib/python3.10/contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/animation.py", line 233, in saving
    self.finish()
  File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/animation.py", line 347, in finish
    raise subprocess.CalledProcessError(
subprocess.CalledProcessError: Command '['convert', '-size', '1280x960', '-depth', '8', '-delay', '3.3333333333333335', '-loop', '0', 'rgba:-', '-layers', 'OptimizePlus', 'FDA Simulation.gif']' returned non-zero exit status 1.

Changing the last line to:

plt.save("FDA Simulation.mov", writer = 'imagemagick', fps = 30)

Outputs this new error:

jacobivanov@Jacob-Ivanovs-MacBook-Air 1D Heat Transfer % /opt/homebr
ew/bin/python3 "/Users/jacobivanov/Desktop/University of Connecticut
/Computational Fluid Dynamics Group/1D Heat Transfer/FDA_Simulation.
py"
Traceback (most recent call last):
  File "/Users/jacobivanov/Desktop/University of Connecticut/Computational Fluid Dynamics Group/1D Heat Transfer/FDA_Simulation.py", line 112, in <module>
    plt.save("FDA Simulation.mov", writer = 'imagemagick', fps = 30)
AttributeError: module 'matplotlib.pyplot' has no attribute 'save'. Did you mean: 'imsave'?
/opt/homebrew/lib/python3.10/site-packages/matplotlib/animation.py:879: UserWarning: Animation was deleted without rendering anything. This is most likely not intended. To prevent deletion, assign the Animation to a variable, e.g. `anim`, that exists until you output the Animation using `plt.show()` or `anim.save()`.

Changing the last line to:

plt.imsave("/Users/jacobivanov/Desktop/University of Connecticut/Computational Fluid Dynamics Group/1D Heat Transfer/FDA Simulation.gif", anim)

Returns this new error:

jacobivanov@Jacob-Ivanovs-MacBook-Air 1D Heat Transfer % /opt/homebr
ew/bin/python3 "/Users/jacobivanov/Desktop/University of Connecticut
/Computational Fluid Dynamics Group/1D Heat Transfer/FDA_Simulation.
py"
Traceback (most recent call last):
  File "/Users/jacobivanov/Desktop/University of Connecticut/Computational Fluid Dynamics Group/1D Heat Transfer/FDA_Simulation.py", line 109, in <module>
    plt.imsave("/Users/jacobivanov/Desktop/University of Connecticut/Computational Fluid Dynamics Group/1D Heat Transfer/FDA Simulation.gif", anim)
  File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/pyplot.py", line 2128, in imsave
    return matplotlib.image.imsave(fname, arr, **kwargs)
  File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/image.py", line 1633, in imsave
    pil_shape = (rgba.shape[1], rgba.shape[0])
AttributeError: 'tuple' object has no attribute 'shape'

I'm not sure how to resolve this, and I've been looking through various Matplotlib animation tutorials without any actual solutions.

  • Is Imagemagick installed? Perhaps remove the space in the gif animation name? What tool is being used for anim.save()? Perhaps you need to do it with it mathplotlib pyplot. – fmw42 Feb 14 '23 at 21:26
  • @Imagemagick, `imagemagick 7.1.0-62 is already installed and up-to-date.`. How would I do it with `pyplot`? – Jacob Ivanov Feb 14 '23 at 21:33
  • What tool is anim.save()? Sorry, I am not an expert on pyplot. But in mathplotlib there is pyplot.save() or fig.save(), if I recall correctly. Did you try removing the space? – fmw42 Feb 14 '23 at 21:39
  • Did you try plt.save("filename.gif", anim) rather than anim.save() – fmw42 Feb 14 '23 at 21:46
  • @fmw42, yes. See my edit. `plt.imsave()` also does not work. – Jacob Ivanov Feb 14 '23 at 21:52
  • Best I can see is that your animation is not in image format, with 2 arguments for the tuple of the shape. – fmw42 Feb 14 '23 at 22:38

0 Answers0