本文介绍了使用 NetworkX 和 Matplotlib 动画网络增长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为随时间增长的图表制作动画.

I would like to animate a graph that grows over time.

这是我目前所拥有的:

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()

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

However, I get the following error message:

 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/

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

What I want is an animation, where you can see the graph growing. I can save the graph at each stage, and might be able to create an animation outside matplotlib, but is there any way of getting it work like this?

推荐答案

经过审查,该代码与此问题的相关性并不像我想象的那么大.但是,我能够使用 this SO answerthis SO answer 为您拼凑出一个答案.下面的代码将创建一个图形,向其中添加 50 个随机节点和 50 个随机边,并在添加每个节点和边后显示图形的图像.代码中的一些关键更改:

Upon review, that code wasn't nearly as relevant to this problem as I'd thought. However, I was able to use this SO answer and this SO answer to cobble together an answer for you. Here's code that will create a graph, add 50 random nodes and 50 random edges to it, and display an image of the graph after every node and edge is added. A few of the key changes from your code:

  1. 此代码基于使用 pylab.ion()(参见 此处了解更多信息).
  2. 您的代码尝试使用一个图形并更改其中的图像.此代码为每一帧创建一个新图.
  3. 此代码不会写入 .mp4.如果您确实需要这样做,我建议您在渲染时将数字写入 .png 文件,然后再转换为 mp4.
  4. 请注意,此代码使用 matplotlib.pyplot.pause() 而不是 time.sleep().如果您使用 time.sleep(),这些数字将不会呈现.
  1. This code is based on using pylab.ion() (see here for more info).
  2. Your code tries to use one figure and change the image within it. This code creates a new figure for each frame.
  3. This code doesn't write out to .mp4. If you really need to do that, I would suggest writing the figures out to .png file while rendering them, and converting to mp4 after the fact.
  4. Note that this code uses matplotlib.pyplot.pause() instead of time.sleep(). The figures won't render if you use 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)

这篇关于使用 NetworkX 和 Matplotlib 动画网络增长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 04:49