使用NetworkX动态扩散图表

5
我希望能在一个图表上进行过程动画(最好使用NetworkX)。我已经看过this question。然而,当我运行解决方案中给出的代码时,我只看到了最终的输出。另外,它没有将动画保存为可用的格式。
假设我们有以下图表:
import networkx as nx

g = nx.Graph()
g.add_edges_from([(1, 2), (2, 3), (1, 3), (1, 4), (3, 4), (4, 5), (5, 9), (4, 9)])

此外,我们有一个初始节点集,我们称之为活动节点:
active = {1, 3}

直观地说,我想做的是动画演示每个活动节点如何在时间上导致图中其他节点变为活动状态。因此,如果我们假设一个模型,在这个模型中,只有当至少两个邻居节点变为活动状态时,每个节点才会变为活动状态。在第二次迭代中,活动节点集将是:
active = {1, 3, 2, 4}

在下一次迭代中,活动节点集将是:
active = {1, 3, 2, 4, 5}.

在最后一次迭代中,图中的所有节点都将变为活动状态:
active = {1, 3, 2, 4, 5, 9}

这个过程被称为“倾斜过程”,是网络中信息扩散的一个例子。您可以在下面看到算法的非常简单的实现。
def tipping(graph, seed_set, thr=2):
    active = seed_set
    has_changed = False
    for n in filter(lambda n: n not in active, graph.nodes()):
        if len(filter(lambda nei: nei in active, graph.neighbors(n))) >= thr:
            active.add(n)
            has_changed = True
    if has_changed:
        return tipping(graph, active, thr) | active
    return active

我想知道是否有任何方法可以将这个过程可视化。我知道可以使用networkX中的nx.draw()函数绘制网络。然而,我没有看到任何可以生成动画或其他有用输出的函数。
一个可能的解决方案是在每个步骤中以不同的节点颜色绘制图形,保存每个步骤的图像,并使用所有保存的图片制作gif动画。
如何使用Networkx对扩散进行动画处理?最好在IPython笔记本中运行动画。

1
一个可能的解决方案是在过程的每个步骤中使用不同节点颜色绘制图形,保存每个图像,并制作包含所有保存图片的GIF动画。这是我建议的做法。 - Joel
2个回答

6

下面是一份简化、剥离的动画示例,对于寻求networkx动画的人应该很有用。如果您运行它,它实际上是有效的。

import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from matplotlib import animation


def simple_update(num, n, layout, G, ax):
    ax.clear()

    # Draw the graph with random node colors
    random_colors = np.random.randint(2, size=n)
    nx.draw(G, pos=layout, node_color=random_colors, ax=ax)

    # Set the title
    ax.set_title("Frame {}".format(num))


def simple_animation():

    # Build plot
    fig, ax = plt.subplots(figsize=(6,4))

    # Create a graph and layout
    n = 30 # Number of nodes
    m = 70 # Number of edges
    G = nx.gnm_random_graph(n, m)
    layout = nx.spring_layout(G)

    ani = animation.FuncAnimation(fig, simple_update, frames=10, fargs=(n, layout, G, ax))
    ani.save('animation_1.gif', writer='imagemagick')

    plt.show()

simple_animation()

你需要一个名为simple_animation的函数来设置和运行动画,还需要一个名为simple_update的函数来更新动画。fargs让你可以传递参数到simple_update函数。

这个动画只是设置随机颜色,你应该能够适应任何其他目的。


你能让动画在jupyter控制台中显示,而不是保存为.gif文件吗?我尝试运行了你的脚本,但只有在我删除def simple_animation()并在主脚本中执行动画而不是在函数内部执行时,才能在jupyter控制台中显示动画。 - Adam Gosztolai
1
抱歉,我不使用Jupyter。 - chasmani

0

我使用matplotlib的animation框架实现了一个动态的Networkx热扩散。如果您在IPython笔记本中安装JSAnimation插件,甚至可以在笔记本中可视化动画!

算法应该是这样的:

import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation

# Optionnal if you want to animate in your notebook
from JSAnimation import IPython_display

def update_func(step, data, nodes):
    # the step parameter is mandatory for matplotlib
    # Set new value for figure data
    # update node color here
    new_data = data + 1
    nodes.set_array(new_data)
    return nodes

def diffuse_anim(inputs, G, data, nb_frames=50):
    fig = plt.figure()
    nodes = nx.draw_networkx_nodes(G, pos, node_size=30, node_color='b')
    return animation.FuncAnimation(fig, update_func, frames=xrange(nb_frames), fargs=(data, nodes))

在我的应用程序中,您可以看到热扩散从不同的源以漂亮的动画在IPython笔记本中传播。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接