【问题标题】:What tool to draw an animated network graph什么工具可以画出动画网络图
【发布时间】:2018-05-16 16:41:44
【问题描述】:

我想获得复杂图上随机游走概率分布的动画。我目前使用 Python 和 NetworkX 来操作图形和评估步行动态。

我的目标是制作一个动画(例如 GIF 文件),其中图形的每个节点的大小与其度数(或其他拓扑属性)成正比,颜色与标量属性(概率分布)成正比。 节点的大小和位置在时间上保持固定,但颜色会发生变化。

目前,我可以使用Gephi 在某个时刻绘制具有所需属性的图形,但我想知道如何制作动画,或者如何自动化生成图像的过程每次瞬间。

有人可以指出一些类似的参考吗?我还可以使用 Gephi 以外的其他可视化工具。实际上,理想情况下,我的所有工作流程都在 Python 中,而无需借助外部程序。

【问题讨论】:

标签: python animation graph networkx


【解决方案1】:

在 matplotlib 中使用 FuncAnimation 相当简单:

import numpy as np
import matplotlib.pyplot as plt; plt.close('all')
import networkx as nx
from matplotlib.animation import FuncAnimation

def animate_nodes(G, node_colors, pos=None, *args, **kwargs):

    # define graph layout if None given
    if pos is None:
        pos = nx.spring_layout(G)

    # draw graph
    nodes = nx.draw_networkx_nodes(G, pos, *args, **kwargs)
    edges = nx.draw_networkx_edges(G, pos, *args, **kwargs)
    plt.axis('off')

    def update(ii):
        # nodes are just markers returned by plt.scatter;
        # node color can hence be changed in the same way like marker colors
        nodes.set_array(node_colors[ii])
        return nodes,

    fig = plt.gcf()
    animation = FuncAnimation(fig, update, interval=50, frames=len(node_colors), blit=True)
    return animation

total_nodes = 10
graph = nx.complete_graph(total_nodes)
time_steps = 20
node_colors = np.random.randint(0, 100, size=(time_steps, total_nodes))

animation = animate_nodes(graph, node_colors)
animation.save('test.gif', writer='imagemagick', savefig_kwargs={'facecolor':'white'}, fps=0.5)

【讨论】:

  • 非常感谢您鼓舞人心的回答。不过,我在进行 blitting 工作时遇到了麻烦。您介意进一步解释node.set_array 步骤吗?我必须重绘,即致电nx.draw_networkx_nodes
  • @jmon12 我不确定你的问题是什么。我建议打开一个引用此答案的新问题并更详细地解释什么不适合您。
  • 我可以解决我自己不值得提问的问题。在我的案例中,我使用了更具体的nodes.set_facecolor,它解决了我的问题。我唯一的改进建议是解释 set_array 的作用以及为什么使用它而不是直接设置颜色。
猜你喜欢
  • 2011-06-02
  • 1970-01-01
  • 2016-11-11
  • 2019-01-19
  • 2015-02-13
  • 1970-01-01
  • 2021-07-13
  • 1970-01-01
  • 2010-12-24
相关资源
最近更新 更多