import matplotlib.pyplot as plt
import time
import networkx as nx
plt.ion() # call interactive on
data = [(0, 1), (1, 2), (1, 4), (1, 9), (2, 6), (3, 5), (5, 7), (5, 8), (5, 10)]
# I create a networkX graph G
G = nx.Graph()
for item, (a, b) in enumerate(data):
G.add_node(item)
G.add_edge(a,b)
# make initial plot and set axes limits.
# (alternatively you may want to set this dynamically)
plot, = plt.plot([], [])
plt.xlim(-1, 3)
plt.ylim(-1, 3)
# here is the plotting stage.
for _ in range(10): # plot 10 times
plt.cla() # clear the previous plot
nx.draw(G)
plt.draw()
time.sleep(2) # otherwise it runs to fast to see
您必须使用
interactive on
和plt.draw
。这里有一个有效的例子。
由于我没有你的算法来生成你的G图,我将粗略地画出相同的网络。不管怎样,每次打给nx.绘制(G) 创建一个不同的图形,您可以看到它在每次调用时更新绘图。在
相关问题 更多 >
编程相关推荐