Python 如何为存储的帧制作networkx动画?

Python 如何为存储的帧制作networkx动画?,python,animation,matplotlib,networkx,Python,Animation,Matplotlib,Networkx,我想根据我已经生成并存储的度量为图表上色。这些将是matplotlib动画的“我的帧”[每帧长度=图形的节点数],该动画应传递给图形的“节点颜色”属性。如何将其转化为动画?下面是一个小型图形的最小非工作示例: # number of nodes size = 10. # generate graph G=nx.complete_graph(size) # generating input frames here, since my data is too big # its importan

我想根据我已经生成并存储的度量为图表上色。这些将是matplotlib动画的“我的帧”[每帧长度=图形的节点数],该动画应传递给图形的“节点颜色”属性。如何将其转化为动画?下面是一个小型图形的最小非工作示例:

# number of nodes
size = 10.

# generate graph
G=nx.complete_graph(size)

# generating input frames here, since my data is too big
# its important that the frames go as input and is not generated 
# on the fly
frame = np.random.random_integers(0, 5, (size, size)) # random ndarray between 0 and 5, length and number of frames = number of nodes in the graph

# draw the topology of the graph, what changes during animation
# is just the color
pos = nx.spring_layout(G)
nodes = nx.draw_networkx_nodes(G,pos)
edges = nx.draw_networkx_edges(G,pos)
plt.axis('off')

# pass frames to funcanimation via update function
# this is where I get stuck, since I cannot break
# out of the loop, neither can I read every array of
# the ndarray without looping over it explicitly
def update(i):
    for i in range(len(frame)):
        # instead of giving frame as input, if I randomly generate it, then it works
        nc =   frame[i] # np.random.randint(2, size=200)
        nodes.set_array(nc)
    return nodes,

# output animation; its important I save it 
ani = FuncAnimation(fig8, update, interval=50,  blit=True)
ani.save('crap.gif', writer='imagemagick',  savefig_kwargs={'facecolor':'white'}, fps=1)
由于上述问题,动画最终只显示最后一帧或第一帧

请注意,如果在update函数中使用nx.draw(),则会出现以下错误:RuntimeError:动画函数必须返回艺术家对象序列


我有一种强烈的感觉,我错过了一些显而易见的简单方法,这个问题是微不足道的。但是因为我不经常使用动画功能,所以我不能完全理解它。

您就快到了:
update
只需要时间t的状态。如果我正确地阅读了您的代码,那么您正在对
update
中的所有
t
进行循环。因此,只需摆脱该循环,并将
frames
参数添加到
FuncAnimation
中,以便它知道要检查的时间

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

# number of nodes
size = 10

# generate graph
G=nx.complete_graph(size)

# generating input frames here, since my data is too big
# its important that the frames go as input and is not generated
# on the fly
frame = np.random.random_integers(0, 5, (size, size)) # random ndarray between 0 and 5, length and number of frames = number of nodes in the graph

# draw the topology of the graph, what changes during animation
# is just the color
pos = nx.spring_layout(G)
nodes = nx.draw_networkx_nodes(G,pos)
edges = nx.draw_networkx_edges(G,pos)
plt.axis('off')

# pass frames to funcanimation via update function
# this is where I get stuck, since I cannot break
# out of the loop, neither can I read every array of
# the ndarray without looping over it explicitly
def update(i):
    # for i in range(len(frame)):
    # instead of giving frame as input, if I randomly generate it, then it works
    nc = frame[i] # np.random.randint(2, size=200)
    nodes.set_array(nc)
    return nodes,

# output animation; its important I save it
fig = plt.gcf()
ani = FuncAnimation(fig, update, interval=50, frames=range(size), blit=True)
ani.save('crap.gif', writer='imagemagick',  savefig_kwargs={'facecolor':'white'}, fps=1)

您就快到了:
更新
只需要时间t的状态。如果我正确地阅读了您的代码,那么您正在对
update
中的所有
t
进行循环。因此,只需摆脱该循环,并将
frames
参数添加到
FuncAnimation
中,以便它知道要检查的时间

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

# number of nodes
size = 10

# generate graph
G=nx.complete_graph(size)

# generating input frames here, since my data is too big
# its important that the frames go as input and is not generated
# on the fly
frame = np.random.random_integers(0, 5, (size, size)) # random ndarray between 0 and 5, length and number of frames = number of nodes in the graph

# draw the topology of the graph, what changes during animation
# is just the color
pos = nx.spring_layout(G)
nodes = nx.draw_networkx_nodes(G,pos)
edges = nx.draw_networkx_edges(G,pos)
plt.axis('off')

# pass frames to funcanimation via update function
# this is where I get stuck, since I cannot break
# out of the loop, neither can I read every array of
# the ndarray without looping over it explicitly
def update(i):
    # for i in range(len(frame)):
    # instead of giving frame as input, if I randomly generate it, then it works
    nc = frame[i] # np.random.randint(2, size=200)
    nodes.set_array(nc)
    return nodes,

# output animation; its important I save it
fig = plt.gcf()
ani = FuncAnimation(fig, update, interval=50, frames=range(size), blit=True)
ani.save('crap.gif', writer='imagemagick',  savefig_kwargs={'facecolor':'white'}, fps=1)

networkx仅使用matplotlib作为其绘图“后端”。我不是100%确定问题是什么,但如果您查看matplotlib colormaps,您可能会找到答案。如果负值被限制为0,则快速修复方法是将
frame
偏移其最小值:
frame-=frame.min()
。networkx仅使用matplotlib作为其图形“后端”。我不是100%确定问题是什么,但如果您查看matplotlib colormaps,您可能会找到答案。如果负值被限制为0,则快速修复方法是将
frame
偏移其最小值:
frame-=frame.min()。