我想做的是创建一个动画,在动画中,图形的节点会随着时间改变颜色。当我在matplotlib上搜索动画相关的信息时,通常看到的示例看起来像这样:
#!/usr/bin/python
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation
fig = plt.figure(figsize=(8,8))
images = []
for i in range(10):
data = np.random.random(100).reshape(10,10)
imgplot = plt.imshow(data)
images.append([imgplot])
anim = ArtistAnimation(fig, images, interval=50, blit=True)
anim.save('this-one-works.mp4')
plt.show()
所以我认为我可以像这样做:
#!/usr/bin/python
import numpy as np
import networkx as nx
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation
G = nx.Graph()
G.add_edges_from([(0,1),(1,2),(2,0)])
fig = plt.figure(figsize=(8,8))
pos=nx.graphviz_layout(G)
images = []
for i in range(10):
nc = np.random.random(3)
imgplot = nx.draw(G,pos,with_labels=False,node_color=nc) # this doesn't work
images.append([imgplot])
anim = ArtistAnimation(fig, images, interval=50, blit=True)
anim.save('not-this-one.mp4')
plt.show()
我卡在的问题是,在使用nx.draw()绘制图形后,我如何获得适当类型的对象放入传递给ArtistAnimation的数组中。在第一个例子中,plt.imshow()返回一个matplot.image.AxesImage类型的对象,但是nx.draw()实际上并没有返回任何东西。有没有办法可以获取适当的图像对象?
当然,完全不同的方法也是欢迎的(似乎在matplotlib中总是有很多不同的方法来做同一件事情),只要我完成后可以将我的动画保存为mp4格式。
谢谢!
--Craig
nx.draw
的返回值吗? - tacaswell