문제 설명
networkx 도면을 업데이트하는 방법 (How to update a networkx drawing)
실시간으로 networkx 플롯을 업데이트하고 싶습니다. 이 답변을 보았지만 아닌 것 같습니다. 도와주거나 제가 잘못 이해했습니다. 여기 내 MWE가 있습니다:
import matplotlib.pyplot as plt
import networkx as nx
import pydot
from networkx.drawing.nx_pydot import graphviz_layout
import time
T = nx.balanced_tree(2, 5)
colors = ['r']*len(T.nodes)
for i in range(31):
colors[i]='y'
pos = graphviz_layout(T, prog="dot")
# Use this instead if you don't have dot installed
# pos = nx.spring_layout(T)
nx.draw(T, pos, node_color=colors, edge_color='#909090', node_size=200, with_labels=True)
plt.show()
time.sleep(1)
plt.clf()
T.remove_edge(3,8)
T.add_edge(3,4)
T.add_edge(7,8)
nx.draw(T, pos, node_color=colors, edge_color='#909090', node_size=200, with_labels=True)
plt.show()
이것을 실행하면 창에 하나의 이미지가 표시되면 창을 종료해야 하고 두 번째 이미지는 새 창에 나타납니다. 전혀 상호 작용할 필요 없이 기존 창에서 업데이트되기를 바랍니다.
어떻게 할 수 있나요?
참조 솔루션
방법 1:
plt.clf()
doesn't work in this situation. When the plt.show()
is runned it will only wait for an interaction to close it, and it won't change.
You should make the changes and delays before the plt.show()
Use plt.pause()
instead.
import matplotlib.pyplot as plt
import networkx as nx
import pydot
from networkx.drawing.nx_pydot import graphviz_layout
import time
T = nx.balanced_tree(2, 5)
colors = ['r']*len(T.nodes)
for i in range(31):
colors[i]='y'
pos = graphviz_layout(T, prog="dot")
# Use this instead if you don't have dot installed
# pos = nx.spring_layout(T)
nx.draw(T, pos, node_color=colors, edge_color='#909090', node_size=200, with_labels=True)
plt.pause(1) # <‑‑‑‑‑‑ it will pause for 1 second
T.remove_edge(3,8)
T.add_edge(3,4)
T.add_edge(7,8)
nx.draw(T, pos, node_color=colors, edge_color='#909090', node_size=200, with_labels=True)
plt.show()
(by graffe、Behdad Abdollahi Moghadam)