我是networkx(python)的初学者,我想绘制一个随时间变化的节点位置的交互式图形。我知道如何修复第一张图的节点位置,但是如何更新节点位置呢?提前谢谢。
答案 0 :(得分:1)
It is not trivial to do so. Networkx is based on matplotlib
scatter (see code here) for nodes and LineCollection
for edges.
To create an animated graph, you should get your inspiration from this matplotlib animated example.
The basic stub should look like this:
# Create new Figure and an Axes which fills it.
fig = plt.figure(figsize=(7, 7))
ax = fig.add_axes([0, 0, 1, 1], frameon=False)
ax.set_xlim(0, 1), ax.set_xticks([])
ax.set_ylim(0, 1), ax.set_yticks([])
scat = nx.draw_nodes(G)
def update(frame_number):
# TODO
# Change node position (x, y) here
scat.set_offsets(new_pos)
# Construct the animation, using the update function as the animation
# director.
animation = FuncAnimation(fig, update, interval=10)
plt.show()