我想绘制由网络networkx for python建立的网络,如下所示:
import networkx as nx
import matplotlib.pyplot as plt
nx.draw(graph, with_labels=True, node_color=setColorMap(graph, nodeDict))
它运作良好,但是我需要每秒更新一次,并且尝试使用
plt.close()
nx.draw(graph, with_labels=True, node_color=setColorMap(graph, nodeDict))
plt.show()
但是那似乎没有用。 我已经在网上搜索并找到了关闭图形并在matplotlib上对其进行更新的方法,但是我无法通过networkx绘图来完成。 可能有一个非常简单的解决方案,我对那些库不感兴趣。
答案 0 :(得分:2)
您应该使用matplotlib动画功能。在更新函数中调用nx.draw()
命令。
此代码的基本草图如下:
# do networkx stuff
fig = plt.figure()
def update(it):
G = graph_list[it]
nx.draw(G, with_labels=True, ...)
ani = animation.FuncAnimation(fig, update, frames=list(range(num_frames)))
plt.show()