网络动画生长的NetworkX和Matplotlib [英] Animating Network Growth with NetworkX and 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?
推荐答案
经审查,即code是几乎没有为我还以为这个问题是相关的。不过,我可以使用<一个href=\"http://stackoverflow.com/questions/6686550/how-to-animate-a-time-ordered-sequence-of-matplotlib-plots\">this SO回答和<一个href=\"http://stackoverflow.com/questions/12822762/pylab-ion-in-python-2-matplotlib-1-1-1-and-updating-of-the-plot-while-the-pro\">this SO回答凑齐一个答案给你。这里code,将创建的曲线图中,加50随机节点和50个随机边缘到它,和每一个节点之后显示图形的图像,并添加边缘。几个从code中的重要变化:
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:
- 这code是基于使用
pylab.ion()
(见的这里获取更多信息)。 - 您$ C $ C尝试使用一个数字,并在其内更改图像。这code会为每一帧新的人物。
- 这code没有写出来.MP4。如果你真的需要做到这一点,我建议写数据到.png文件,同时使它们,并在以后转换为MP4。
- 请注意,这code使用
matplotlib.pyplot.pause()
而不是time.sleep()
。如果你使用time.sleep的数字将不会呈现()
。
- This code is based on using
pylab.ion()
(see here for more info). - Your code tries to use one figure and change the image within it. This code creates a new figure for each frame.
- 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.
- Note that this code uses
matplotlib.pyplot.pause()
instead oftime.sleep()
. The figures won't render if you usetime.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!