Jonathan Bechtel
Jonathan Bechtel

Reputation: 3617

Saving Animated Matplotlib Chart to GIF

I have an animated matplotlib chart that I'm trying to save to a .gif file, but I can't get my image writer to work.

I've installed imagemagick and verified that it works on my computer via the instructions given on its installation page, but when I do this:

anim.save('gd.gif', writer='imagemagick')

I get the following error message:

MovieWriter imagemagick unavailable. Trying to use pillow instead.

However, doing anim.save('gd.gif', writer='pillow') gives the following error message:

ValueError: not enough image data

I tried installing ffmpeg with the command conda install -c conda-forge ffmpeg. It looked like it installed correctly, but I don't know how to bind it to matplotlib apparently.

Specifying the writer as ffmpeg gives the same error message that I encountered with imagemagick.

I also tried adding imagemagick's path to matplotlib's config file path with the following line:

animation.convert_path: 'C:\Program Files\ImageMagick-7.0.8-Q16\magick.exe'

That was suggested in this question.

None of these seemed to have worked though.

I'm on Windows 10, and am using Python 3.7

Upvotes: 1

Views: 9236

Answers (2)

Monobakht
Monobakht

Reputation: 303

A quick solve is to simply add plt.show() before running anim.save('gd.gif', writer='imagemagick'). When you close the plot it will save the animation correctly.

Upvotes: 1

Rolf of Saxony
Rolf of Saxony

Reputation: 22458

I pinched this code from the interweb, which initially didn't work.
I changed the line:

rcParams['animation.convert_path'] = r'/usr/bin/convert'

to point to my convert binary and it fired up. Admittedly, this is on Linux but I don't see that it should differ. As noted by Mark Setchell in his comment to the original question, it appears that the name of the binary may well depend on the version/operating system that you are using. To isolate which one to use, I'd suggest trying the command on the command line first.
e.g. For me on Linux:

convert -version

gives:

Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 http://www.imagemagick.org

Your mileage may vary! Especially as I can no longer remember much about issuing terminal commands on a Windows OS.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import rcParams

# configure full path for ImageMagick
rcParams['animation.convert_path'] = r'/usr/bin/convert'

TWOPI = 2*np.pi

fig, ax = plt.subplots()

t = np.arange(0.0, TWOPI, 0.001)
s = np.sin(t)
l = plt.plot(t, s)

ax = plt.axis([0,TWOPI,-1,1])

redDot, = plt.plot([0], [np.sin(0)], 'ro')

def animate(i):
    redDot.set_data(i, np.sin(i))
    return redDot,

# create animation using the animate() function with no repeat
myAnimation = animation.FuncAnimation(fig, animate, frames=np.arange(0.0, TWOPI, 0.1), \
                                      interval=10, blit=True, repeat=False)

# save animation at 30 frames per second
myAnimation.save('myAnimation.gif', writer='imagemagick', fps=30)

enter image description here

Upvotes: 3

Related Questions