【问题标题】:Trouble with Networkx + Matplotlib Animations - 'NoneType' object has no attribute 'set_visible'Networkx + Matplotlib 动画的问题 - 'NoneType' 对象没有属性 'set_visible'
【发布时间】:2021-01-06 17:20:05
【问题描述】:

我在运行以下 DFS 动画时遇到了一些问题。 我相信这可能是因为没有背景画布,但我不确定如何解决这个问题,因为所有其他类似的在线实现都使用 plt.plot 而不是 nx.draw 来保存要显示的图像。

有人可以提供任何指导吗?

import networkx as nx
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation

fig = plt.figure()
ax = plt.gca()
colors = [0]*len(g)
cmap = plt.get_cmap("autumn")

g = nx.random_tree(20)
pos = nx.fruchterman_reingold_layout(g, k=0.1)


ims = [[nx.draw_networkx(g, pos, node_color = colors, cmap = cmap, ax = ax, vmin=0.0, vmax=1.0)]]

artists = [(nx.draw_networkx(g, pos, node_color = colors, cmap = cmap, ax = ax, vmin=0.0, vmax=1.0),)]

stack = [0] 

while stack:
    node = stack.pop()
    
    if colors[node]: 
        continue
        
    colors[node] = 0.8
    stack += list(g[node].keys())
    
    img = nx.draw_networkx(g, pos, node_color=colors, cmap=cmap, ax=ax, vmin=0.0, vmax=1.0)
    ims += [img]
    
anim = ArtistAnimation(fig, ims, blit = True)
# plt.show()

【问题讨论】:

    标签: matplotlib animation networkx


    【解决方案1】:

    您的代码的问题是nx.draw_networkx() 不返回任何内容,而使用FuncAnimation 方法总是更容易。首先,您需要创建一个颜色生成器,以使动画函数切换到每次调用设置的下一个颜色。然后使用FuncAnimation 你可以为你的帧(情节)设置动画:

    import networkx as nx
    import matplotlib.pyplot as plt
    import matplotlib
    import matplotlib.animation as animation
    
    matplotlib.use('TkAgg')
    plt.ion()
    
    g = nx.random_tree(20)
    colors = [0] * len(g)
    cmap = plt.get_cmap('autumn')
    pos = nx.fruchterman_reingold_layout(g, k=0.1)
    
    # here you make the generator
    def change_colors():
        stack = [0]
        
        yield colors
    
        while stack:
            node = stack.pop()
            if colors[node]:
                continue
    
            colors[node] = 0.8
            stack += list(g[node].keys())
    
            yield colors
    
    # instantiate your generator
    color_gen = change_colors()
    
    
    def update_frame(n):
        # clear the plot
        plt.cla()
        # here switch to the next colors
        colors = next(color_gen)
        # then draw
        nx.draw(g, pos, with_labels=True, node_color=colors, cmap=cmap, vmin=0.0, vmax=1.0)
    
    
    ani = animation.FuncAnimation(plt.gcf(), update_frame, repeat=False, interval=1000)
    
    plt.ioff()
    plt.show()
    

    这将为您提供: 您可以通过将plt.show() 替换为ani.save('anim.gif', writer='imagemagick') 来将其保存为gif 文件。

    【讨论】:

    • 感谢您的代码!我可以看到它对你有用,但是在尝试 ani.save('anim.gif', writer='imagemagick') 时,我得到 'str' object is not callable on the filename。
    • 所以情节有效,您只能将其保存为 gif 吗?如果是,你能告诉我你是如何保存它的吗?叫什么名字?
    • 我使用的是 google colab,所以我评论了 mat Tkagg,以及 plt.ion 和 plt.ioff 行。所有其他代码都与您的相同,并且 plt.show() 被 ani.save(filename = 'anim.gif', fps = 60) 替换。这会产生以下错误: MovieWriter stderr: [gif @ 000001da8bbca380] GIF muxer 仅支持单个视频 GIF 流。无法为输出文件 #0 写入标头(编解码器参数不正确?):无效参数 初始化输出流时出错 0:0 --
    • 导入库后在顶部添加%matplotlib notebook并使用ani.save('anim.gif', writer='imagemagick'),它会给你StopIteration错误,但是你想要的gif将在你当前的笔记本文件夹中创建。跨度>
    • 请注意,我还在change_colors 生成器的开头添加了另一个yield 语句,以免跳过第一帧。
    猜你喜欢
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    相关资源
    最近更新 更多