【问题标题】:Error exporting animation to gif - Matplotlib将动画导出为 gif 时出错 - Matplotlib
【发布时间】:2020-04-03 08:24:29
【问题描述】:

我的目标是将动画导出为 gif 格式。我可以使用 mp4 来实现这一点,但在转换为 gif 时出现错误。我不确定是脚本错误还是某些后端设置。

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import animation

df1 = pd.DataFrame({
    'Time' : [1,1,1,2,2,2,3,3,3],        
    'GroupA_X' : [3, 4, 5, 12, 15, 16, 21, 36, 47], 
    'GroupA_Y' : [2, 4, 5, 12, 15, 15, 22, 36, 45],            
    'GroupB_X' : [2, 5, 3, 12, 14, 12, 22, 33, 41],   
    'GroupB_Y' : [2, 4, 3, 13, 13, 14, 24, 32, 45],                 
        })

fig, ax = plt.subplots()
ax.grid(False)
ax.set_xlim(0,50)
ax.set_ylim(0,50)

def groups():

    Group_A = df1[['Time','GroupA_X','GroupA_Y']]

    GA_X = np.array(Group_A.groupby(['Time'])['GroupA_X'].apply(list))
    GA_Y = np.array(Group_A.groupby(['Time'])['GroupA_Y'].apply(list))

    GA = ax.scatter(GA_X[0], GA_Y[0], c = ['blue'], marker = 'o', s = 10, edgecolor = 'black')

    return GA, GA_X, GA_Y

def animate(i) :

    GA, GA_X, GA_Y = groups()

    GA.set_offsets(np.c_[GA_X[0+i], GA_Y[0+i]])


ani = animation.FuncAnimation(fig, animate, np.arange(0,3), interval = 1000, blit = False)

# If exporting as an mp4 it works fine.

#Writer = animation.writers['ffmpeg']
#writer = Writer(fps = 10, bitrate = 8000)
#ani.save('ani_test.mp4', writer = writer)


#But if I try to export as a gif it returns an error:

ani.save('gif_test.gif', writer = 'imagemagick')

错误:

MovieWriter imagemagick unavailable. Trying to use pillow instead.

self._frames[0].save(
IndexError: list index out of range

注意:我也尝试过以下返回相同的Index error

my_writer=animation.PillowWriter(fps = 10)
ani.save(filename='gif_test.gif', writer=my_writer)

我尝试从其他问题animate gif 调整许多设置。我当前的动画设置如下。我使用的是 Mac。

###ANIMATION settings
#animation.html :  none            ## How to display the animation as HTML in
                                   ## the IPython notebook. 'html5' uses
                                   ## HTML5 video tag; 'jshtml' creates a 
                                   ## Javascript animation
#animation.writer : imagemagick         ## MovieWriter 'backend' to use
#animation.codec : mpeg4            ## Codec to use for writing movie
#animation.bitrate: -1             ## Controls size/quality tradeoff for movie.
                                   ## -1 implies let utility auto-determine
#animation.frame_format:  png      ## Controls frame format used by temp files
#animation.html_args:              ## Additional arguments to pass to html writer
animation.ffmpeg_path: C:\Program Files\ImageMagick-6.9.1-Q16\ffmpeg.exe    ## Path to ffmpeg binary. Without full path
                                   ## $PATH is searched
#animation.ffmpeg_args:            ## Additional arguments to pass to ffmpeg
#animation.avconv_path:  avconv    ## Path to avconv binary. Without full path
                                   ## $PATH is searched
#animation.avconv_args:            ## Additional arguments to pass to avconv
animation.convert_path: C:\Program Files\ImageMagick-6.9.2-Q16-HDRI    ## Path to ImageMagick's convert binary.
                                   ## On Windows use the full path since convert
                                   ## is also the name of a system tool.
#animation.convert_args:           ## Additional arguments to pass to convert
#animation.embed_limit : 20.0

【问题讨论】:

  • 对我来说没有错误。效果很好。来自 IDE 的问题?
  • 干杯。我想可能是这样。我会再做一些搜索
  • 您说您使用的是 Mac,但这些是为 animation.ffmpeg_path 和 animation.convert_path 设置的 Windows 路径。这是为什么呢?
  • 我关注了另一个问题,并认为它们适用于 Mac。我会仔细检查
  • @WilliamMiller,能否请您指出正确的方向以找到适用于 Mac 的相关设置

标签: python matplotlib gif


【解决方案1】:

你已经配置的路径,

animation.ffmpeg_path: C:\Program Files\ImageMagick-6.9.1-Q16\ffmpeg.exe

和

animation.convert_path: C:\Program Files\ImageMagick-6.9.2-Q16-HDRI

适用于 Windows,但由于您使用的是 Mac,因此您需要 MacOS 的路径。您应该能够从终端使用which 获取它们。在我的 Ubuntu 上安装 which 提供以下内容

>$ which convert
/usr/bin/convert

>$ which ffmpeg  
/usr/bin/ffmpeg

MacOS 应该类似。这些是需要提供给 rcParams animation.convert_path 和 animation.ffmpeg_path 的路径,即

animation.ffmpeg_path: /usr/bin/ffmpeg
animation.convert_path: /usr/bin/convert

请注意,虽然 matplotlib 配置中的路径错误会产生相关错误,但修复它可能无法解决错误 - 也可能存在其他错误。

【讨论】:

  • 那么在我的命令终端中插入>$ which convert会显示正确的路径吗?
  • @jonboy >$ 不是命令的一部分,这正是我配置提示的方式。命令是which convert 和which ffmpeg。
  • 谢谢。我可以找到ffmpeg 的路径,但convert 没有返回任何内容。
  • @jonboy 这表明 PATH 变量或 imagemagick 安装存在问题,您可以通过卸载并重新安装 imagemagick 来解决此问题
  • 感谢@William Miller。下载imagemagick时遇到各种问题。尝试重新安装 brew 时出现错误。错误:Need sudo access on macOS! >>> /etc/sudoers: syntax error near line 47 <<< >>> /etc/sudoers: syntax error near line 49 <<< sudo: parse error in /etc/sudoers near line 47 sudo: no valid sudoers sources found, quitting sudo: unable to initialize policy plugin
【解决方案2】:

我从类似question 的帖子中找到了解决方案。似乎 PillowWriter 类是在我的计算机上工作的,我无法克服 ImageMagick 类引起的错误。您可能对设置比特率和编解码器有更好的了解,这些是我之前提到的问题的猜测或复制。

ani = animation.FuncAnimation(fig, new_animate, frames=np.arange(0, 3)
plt.show()

my_writer=animation.PillowWriter(fps=20, codec='libx264', bitrate=2)

ani.save(filename='gif_test.gif', writer=my_writer)

【讨论】:

    猜你喜欢
    • 2018-03-24
    • 2018-12-31
    • 2021-01-29
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    相关资源
    最近更新 更多