Convert LineCollection to an array or Networkx graphic animation

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

A simple code could be:

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

python collection is output:

<matplotlib.collections.LineCollection at 0xd47d9d0>

I want to create a list of these drawings:

artists=[]
artists.append(Drawing)

And then use these pictures in the 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 shown below:

TypeError: 'LineCollection' object is not iterable

, , "" , , png-, - PIL ( ), , , png .

, : , im = plt.imshow(f(x, y)) , , :

TypeError: Image data can not convert to float

, , . - ?

+4
1

( iPython , ). , draw_networkx , . , (pos ).

%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()
+2

Source: https://habr.com/ru/post/1545828/


All Articles