【问题标题】:matplotlib ArtistAnimation returns a blank videomatplotlib ArtistAnimation 返回一个空白视频
【发布时间】:2017-08-21 23:12:39
【问题描述】:

我正在尝试制作随时间变化的 networkx 图的动画。我正在使用 networkx_draw 实用程序来创建图表的 matplotlib 图形,并使用 matplotlib 的 ArtistAnimation 模块从艺术家 networkx 制作的动画中创建动画。我已经对我在这里所做的事情进行了最低限度的复制:

import numpy as np
import networkx as nx
import matplotlib.animation as animation
import matplotlib.pyplot as plt

# Instantiate the graph model
G = nx.Graph()
G.add_edge(1, 2)

# Keep track of highest node ID
G.maxNode = 2

fig = plt.figure()
nx.draw(G)
ims = []

for timeStep in xrange(10):

    G.add_edge(G.maxNode,G.maxNode+1)
    G.maxNode += 1

    pos = nx.drawing.spring_layout(G)
    nodes = nx.drawing.draw_networkx_nodes(G, pos)
    lines = nx.drawing.draw_networkx_edges(G, pos)

    ims.append((nodes,lines,))
    plt.pause(.2)
    plt.cla()

im_ani = animation.ArtistAnimation(fig, ims, interval=200,            repeat_delay=3000,blit=True)
im_ani.save('im.mp4', metadata={'artist':'Guido'})

该过程在实时显示数字时运行良好,它会产生我想要的动画。它甚至在脚本末尾的图形中生成循环动画,这也是我想要的,这表明动画过程有效。但是,当我打开保存到磁盘的“im.mp4”文件时,它是一个空白的白色图像,运行了预期的时间,从不显示任何实时显示的图形图像。

我使用的是 networkx 1.11 版和 matplotlib 2.0 版。我正在使用 ffmpeg 制作动画,并且在 Mac、OSX 10.12.3 上运行。

我做错了什么?

【问题讨论】:

    标签: python animation matplotlib ffmpeg networkx


    【解决方案1】:

    简短的回答:如果你不想有一个空的动画,不要清除轴! IE。删除行plt.cla()。然后,您还应该删除初始的nx.draw(G),因为它不会添加到ims 数组中,否则会停留在动画的每一帧中。

    原因和更详细的解释可以在这个问题中找到,
    Matplotlib video creation,其中处理了类似的案例。

    当然,缺点是当删除plt.cla() 时,屏幕上会出现拥挤的动画;所以你需要事先决定是在屏幕上绘图还是保存。

    【讨论】:

      猜你喜欢
      • 2022-11-12
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多