【发布时间】:2014-08-14 16:07:37
【问题描述】:
我正在尝试使用 networkx.draw 函数的输出,它是一个集合 (LineCollection),用于需要数组的 matplotlib.animation。我不想将我的图形保存为 png,因为它们会很多。我也不想显示它,但这并不重要。
一个简单的代码可以是:
import networkx as nx
graph= nx.complete_graph(5) #a simple graph with five nodes
Drawing=nx.draw(graph)
这会输出一个 python 集合:
<matplotlib.collections.LineCollection at 0xd47d9d0>
我想创建一个此类图纸的列表:
artists=[]
artists.append(Drawing)
并在动画中进一步使用这些绘图:
import matplotlib
fig= plt.figure() #initial figure, which can be empty
anim=matplotlib.animation.ArtistAnimation(fig, artists,interval=50, repeat_delaty=1000)
但是我得到一个 TypeError 如下:
TypeError: 'LineCollection' object is not iterable
所以,我认为“艺术家”列表应该是图像列表,应该是 numpy 数组或 png 图像或称为 PIL 的东西(我不熟悉),我不知道如何转换集合到其中一个没有将图像保存为 png 或任何其他格式的图像。
实际上这就是我想要做的:a dynamic animation,当我尝试将im = plt.imshow(f(x, y)) 与我拥有的一张图纸一起使用时,它给出了这个错误:
TypeError: Image data can not convert to float
我希望我足够清楚,这是我第一次使用动画和绘图工具。有人有解决办法吗?
【问题讨论】:
标签: python animation numpy matplotlib networkx