Ash*_*kan 5 python animation graph matplotlib networkx
我想在图(最好在 NetworkX 中)上为一个过程设置动画。我已经看到了这个问题。但是,当我运行解决方案中给出的代码时,我只能看到最终输出。此外,这不会以某种可用格式保存动画。
假设我们有以下图表:
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)])
Run Code Online (Sandbox Code Playgroud)
此外,我们有一组初始节点,我们称之为活动节点:
active = {1, 3}
Run Code Online (Sandbox Code Playgroud)
直观地说,我想要做的是动画每个活动节点将如何导致图中的其他节点及时变为活动状态。因此,如果我们假设一个模型,如果每个节点至少有两个相邻节点变为活动节点,则该模型将变为活动节点,在第二次迭代中,活动节点集将是:
active = {1, 3, 2, 4}
Run Code Online (Sandbox Code Playgroud)
在下一次迭代中,活动节点集将是:
active = {1, 3, 2, 4, 5}.
Run Code Online (Sandbox Code Playgroud)
在最后一次迭代中,图中的所有节点都将变为活动状态:
active = {1, 3, 2, 4, 5, 9}
Run Code Online (Sandbox Code Playgroud)
这个过程被称为倾销过程,是网络中信息传播的一个例子。您可以在下面看到该算法的一个非常简单的实现。
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
Run Code Online (Sandbox Code Playgroud)
我想知道是否有任何方法可以形象化这个过程。我知道我可以使用nx.draw()networkX 中的函数绘制网络。但是,我还没有看到任何为此场景生成动画或任何其他有用输出的函数。
一种可能的解决方案可能是在过程的每个步骤中用不同的节点颜色绘制图形,保存它们中的每一个,并使用所有保存的图片制作 gif 动画。
如何使用 Networkx 为扩散设置动画?动画最好在 IPython notebook 中运行。
这是一个简化的、精简的动画示例,对任何寻找 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()
Run Code Online (Sandbox Code Playgroud)
您需要一个函数simple_animation来设置和运行动画,以及一个函数simple_update来更新动画。fargs 允许您将参数传递给simple_update函数。
这个动画只是设置随机颜色,你应该能够将它调整到任何其他目的。
我使用 matplotlib 框架实现了一个动画 Networkx 热扩散animation。如果您为 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))
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,您可以在 IPython 笔记本的精美动画中看到从不同来源传播的热扩散。
| 归档时间: |
|
| 查看次数: |
3864 次 |
| 最近记录: |