gpt4 book ai didi

python - 将动画 Matplotlib 图表保存为 GIF

转载 作者:行者123 更新时间:2023-12-04 22:53:09 33 4
gpt4 key购买 nike

我有一个动画 matplotlib 图表,我正试图将其保存到 .gif 文件中,但我无法让我的图像编写器工作。

我已经安装了 imagemagick 并通过它的 installation page 上给出的说明验证它在我的计算机上工作。 ,但是当我这样做时:

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

我收到以下错误消息:
MovieWriter imagemagick unavailable. Trying to use pillow instead.

然而,做 anim.save('gd.gif', writer='pillow')给出以下错误消息:
ValueError: not enough image data

我尝试使用命令 conda install -c conda-forge ffmpeg 安装 ffmpeg .看起来它安装正确,但我显然不知道如何将它绑定(bind)到 matplotlib。

将编写器指定为 ffmpeg 会给出我在使用 imagemagick 时遇到的相同错误消息。

我还尝试使用以下行将 imagemagick 的路径添加到 matplotlib 的配置文件路径:
animation.convert_path: 'C:\Program Files\ImageMagick-7.0.8-Q16\magick.exe'

那是 suggested in this question .

但这些似乎都没有奏效。

我在 Windows 10 上,并且正在使用 Python 3.7

最佳答案

我从互联网上截取了这段代码,最初没有用。
我改变了这一行:

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

指向我的 convert二进制文件,它启动了。
诚然,这是在 Linux 上,但我不认为它应该有所不同。
正如 Mark Setchell 在对原始问题的评论中指出的那样, binary 的名称似乎很可能取决于您使用的版本/操作系统。要隔离使用哪一个,我建议尝试 command line 上的命令第一的。
例如在 Linux 上对我来说:
convert -version

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

你的旅费可能会改变!尤其是当我不再记得在 Windows 操作系统上发出终端命令时。
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

关于python - 将动画 Matplotlib 图表保存为 GIF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56423221/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com