使用NetworkX和Matplotlib实现网络增长动画

Nic*_*oto 14 python animation matplotlib networkx

我想动画一段随着时间的推移而增长的图表.

这是我到目前为止:

fig = plt.figure()
ims = []
graph = nx.Graph()
for i in range(50):
    // Code to modify Graph
    nx.draw(graph, pos=nx.get_node_attributes(graph,'Position'))
    im = plt.draw()
    self.ims.append([im])
ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,repeat_delay=1000)
ani.save('dynamic_images.mp4')
plt.show()
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误消息:

 File "main.py", line 204, in <module>
    repeat_delay=1000)
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 356, in __init__
    TimedAnimation.__init__(self, fig, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 304, in __init__
    Animation.__init__(self, fig, event_source=event_source, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 53, in __init__
    self._init_draw()
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 363, in _init_draw
    artist.set_visible(False)
AttributeError: 'NoneType' object has no attribute 'set_visible'
nicomoto@nicomoto-VirtualBox:~/Desktop/CS8903-SpecialProblem/Code/
Run Code Online (Sandbox Code Playgroud)

我想要的是动画,你可以看到图形在增长.我可以在每个阶段保存图形,也可以在matplotlib之外创建一个动画,但有没有办法让它像这样工作?

sad*_*hen 14

bretlance的改进版本.希望它会有所帮助.它将显示动画,但不会显示图片.

仍然不知道Animate绘制networkx边缘问题的所有者如何 利用matplotlib的动画

#!/usr/bin/env python
import random
import pylab
from matplotlib.pyplot import pause
import networkx as nx
pylab.ion()

graph = nx.Graph()
node_number = 0
graph.add_node(node_number, Position=(random.randrange(0, 100), random.randrange(0, 100)))

def get_fig():
    global node_number
    node_number += 1
    graph.add_node(node_number, Position=(random.randrange(0, 100), random.randrange(0, 100)))
    graph.add_edge(node_number, random.choice(graph.nodes()))
    nx.draw(graph, pos=nx.get_node_attributes(graph,'Position'))

num_plots = 50;
pylab.show()

for i in range(num_plots):

    get_fig()
    pylab.draw()
    pause(2)
Run Code Online (Sandbox Code Playgroud)


bre*_*nce 9

经过审核,该代码与我想到的问题几乎没有相关性.但是,我能够使用这个SO答案这个SO答案为你拼凑一个答案.这里的代码将创建一个图形,添加50个随机节点和50个随机边缘,并在添加每个节点和边缘后显示图形的图像.您的代码中的一些主要更改:

  1. 此代码基于使用pylab.ion()(有关详细信息,请参阅此处).
  2. 您的代码尝试使用一个数字并更改其中的图像.此代码为每个帧创建一个新图.
  3. 此代码不会写入.mp4.如果你真的需要这样做,我建议在渲染它们时将数字写入.png文件,并在事后转换为mp4.
  4. 请注意,此代码使用matplotlib.pyplot.pause()而不是time.sleep().如果您使用,这些数字将无法呈现time.sleep().

祝好运!

import random
import pylab
from matplotlib.pyplot import pause
import networkx as nx
pylab.ion()

graph = nx.Graph()
node_number = 0
graph.add_node(node_number, Position=(random.randrange(0, 100), random.randrange(0, 100)))

def get_fig():
    global node_number
    node_number += 1
    graph.add_node(node_number, Position=(random.randrange(0, 100), random.randrange(0, 100)))
    graph.add_edge(node_number, random.choice(graph.nodes()))
    fig = pylab.figure()
    nx.draw(graph, pos=nx.get_node_attributes(graph,'Position'))
    return fig

num_plots = 50;
pylab.show()

for i in range(num_plots):

    fig = get_fig()
    fig.canvas.draw()
    pylab.draw()
    pause(2)
    pylab.close(fig)
Run Code Online (Sandbox Code Playgroud)


Joh*_*anC 6

由于现有的答案相当旧,因此以下是当前版本的 matplotlib 和 networkx 的代码的样子。

import random
import networkx as nx
import matplotlib.pyplot as plt

graph = nx.Graph()
num_plots = 50
for node_number in range(num_plots):
    graph.add_node(node_number, Position=(random.randrange(0, 100), random.randrange(0, 100)))
    graph.add_edge(node_number, random.choice(list(graph.nodes())))
    nx.draw(graph, pos=nx.get_node_attributes(graph,'Position'))
    plt.pause(0.5)
Run Code Online (Sandbox Code Playgroud)