【发布时间】:2021-01-06 17:20:05
【问题描述】:
我在运行以下 DFS 动画时遇到了一些问题。 我相信这可能是因为没有背景画布,但我不确定如何解决这个问题,因为所有其他类似的在线实现都使用 plt.plot 而不是 nx.draw 来保存要显示的图像。
有人可以提供任何指导吗?
import networkx as nx
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation
fig = plt.figure()
ax = plt.gca()
colors = [0]*len(g)
cmap = plt.get_cmap("autumn")
g = nx.random_tree(20)
pos = nx.fruchterman_reingold_layout(g, k=0.1)
ims = [[nx.draw_networkx(g, pos, node_color = colors, cmap = cmap, ax = ax, vmin=0.0, vmax=1.0)]]
artists = [(nx.draw_networkx(g, pos, node_color = colors, cmap = cmap, ax = ax, vmin=0.0, vmax=1.0),)]
stack = [0]
while stack:
node = stack.pop()
if colors[node]:
continue
colors[node] = 0.8
stack += list(g[node].keys())
img = nx.draw_networkx(g, pos, node_color=colors, cmap=cmap, ax=ax, vmin=0.0, vmax=1.0)
ims += [img]
anim = ArtistAnimation(fig, ims, blit = True)
# plt.show()
【问题讨论】:
标签: matplotlib animation networkx