4

I am trying to use the output from networkx.draw function, which is a collection (LineCollection), to use in matplotlib.animation that requires an array. I dont want to save my figure as png, because there will be a lot of them. I also dont want to display it but it is not crucial.

A simple code can be:

import networkx as nx
graph= nx.complete_graph(5) #a simple graph with five nodes
Drawing=nx.draw(graph)

this outputs a python collection:

<matplotlib.collections.LineCollection at 0xd47d9d0>

I want to create a list of those kind of Drawings:

artists=[]
artists.append(Drawing)

And further use those drawings in an animation:

import matplotlib
fig= plt.figure()  #initial figure, which can be empty
anim=matplotlib.animation.ArtistAnimation(fig, artists,interval=50, repeat_delaty=1000)

However i get a TypeError as below:

TypeError: 'LineCollection' object is not iterable

So, I figured the "artists" list should be a list of images which should either be numpy arrays or a png image or something called PIL (which I am not familiar with), and I dont know how to convert a collection to one of those without saving the image as png or any other format.

Actually this is what i want to do: a dynamic animation, when I try to use im = plt.imshow(f(x, y)) with one of the drawings I have, it gives this error:

TypeError: Image data can not convert to float

I hope I was clear enough, this is my first time with animation and plotting tools. Does anyone have a solution?

potato
  • 71
  • 7

1 Answers1

2

Here is a dynamic animation (that works in iPython notebook if you want to see it that way). Essentially, you want to use draw_networkx and provide it with the items to be plotted for each frame. To prevent the positions from changing on every call to that function, you want to reuse the same positions (pos below).

%pylab inline  #ignore out of ipython notebook
from IPython.display import clear_output #ignore out of ipython notebook

import networkx as nx
graph= nx.complete_graph(5) #a simple graph with five nodes

f, ax = plt.subplots()

pos=nx.spring_layout(graph)

for i in range(5):
    nx.draw_networkx(graph, {j:pos[j] for j in range(i+1)}, ax=ax, nodelist=graph.nodes()[0:i+1], 
                     edgelist=graph.edges()[0:i], with_labels=False)
    ax.axis([-2,2,-2,2]) #can set this by finding max/mins

    time.sleep(0.05)
    clear_output(True) #for iPython notebook
    display(f)
    ax.cla() # turn this off if you'd like to "build up" plots

plt.close()
ChrisE
  • 33
  • 5
  • I tried your code with Ipython, it gives outputs like this '' without displaying. Your code does not clear the display but only clearing the axis, I couldnt understand why we should re-write the axis every time. And what I want to do is clearing the image and re-plotting it. I tried it myself with 'plt.ion()' and 'plt.show()' with 'clf()', python display crashes for some reason, and that does not help me saving the animation, any ideas why? And I was also using the position as 'nx.draw_network(graph, pos)',does it differ from it? – potato Jun 25 '14 at 09:41
  • I just used an idea from [this answer](http://stackoverflow.com/a/13571425/3482094) and used `pause()` instead of timer, and `pylab.draw()` instead of `plt.show`, I dont know why but it worked! So you can ignore previous questions. But still couldnt solve the video problem. – potato Jun 25 '14 at 10:44
  • Sorry, I'm not sure what you mean by the video problem? If you uncomment the 2nd last line [here](http://matplotlib.org/examples/animation/dynamic_image2.html), you should get video – ChrisE Jun 25 '14 at 19:41
  • That example in matplotlib website uses a function to create every image, which outputs an array. matplotlib.animation.ArtistAnimation needs an numpy array or png image to create an animation. In my case networkx outputs collections, which I cannot use, so I have to save them as png. But I dont want to do it, because there will be lots of images. My original question was how can I convert the collections to arrays or sth equivalent to png without saving every new image; so that I can use it to create a video. – potato Jun 26 '14 at 14:48