import numpy as np
import networkx as nx
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
G = nx.Graph()G.add_edges_from([(0,1),(1,2),(2,0)])fig = plt.figure(figsize=(8,8))pos=nx.graphviz_layout(G)nc = np.random.random(3)nodes = nx.draw_networkx_nodes(G,pos,node_color=nc)edges = nx.draw_networkx_edges(G,pos)def update(n): nc = np.random.random(3) nodes.set_array(nc) return nodes,anim = FuncAnimation(fig, update, interval=50, blit=True)
nx.draw不返回任何内容,因此为什么您的方法不起作用。最简单的方法是绘制
nodes和
edges使用
nx.draw_networkx_nodes以及
nx.draw_networkx_edges返回
PatchCollection和
LineCollection对象。然后,您可以使用来更新节点的颜色
set_array。
使用相同的总体框架的工作,你也可以左右移动节点(通过
set_offsets的
PatchCollection和
set_verts或
set_segments为
LineCollection)
我见过的最好的动画教程:http : //jakevdp.github.io/blog/2012/08/18/matplotlib-
animation-tutorial/



