我想在while循环中绘制图形,但执行在 plt.show(G)命令后被阻止,并在我手动终止绘图窗口时恢复。
代码:
while True:
print G.edges()
Q = #coming from a function
if Q > BestQ:
nx.draw(G)
plt.show(G)
if G.number_of_edges() == 0:
break
这是两次迭代的 G.edges()的输出:
[(0, 1), (1, 2), (1, 4), (1, 9), (2, 6), (3, 5), (5, 7), (5, 8), (5, 10)]
[(0, 1), (1, 4), (1, 9), (2, 6), (3, 5), (5, 7), (5, 8), (5, 10)]
如何在绘制???
之后继续这样做答案 0 :(得分:1)
您必须使用interactive on
和plt.draw
这里有一个有效的例子
由于我没有你的算法来生成你的G图,我将连续绘制相同的网络。无论如何,当每次调用nx.draw(G)创建一个不同的图形时,您可以看到它在每次调用时更新图形。
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