python - Converting LineCollection to array OR animating Networkx graph -
i trying utilize output networkx.draw function, collection (linecollection), utilize in matplotlib.animation requires array. dont want save figure png, because there lot of them. dont want display not crucial.
a simple code can be:
import networkx nx graph= nx.complete_graph(5) #a simple graph 5 nodes drawing=nx.draw(graph) this outputs python collection:
<matplotlib.collections.linecollection @ 0xd47d9d0> i want create list of kind of drawings:
artists=[] artists.append(drawing) and farther utilize drawings in animation:
import matplotlib fig= plt.figure() #initial figure, can empty anim=matplotlib.animation.artistanimation(fig, artists,interval=50, repeat_delaty=1000) however typeerror below:
typeerror: 'linecollection' object not iterable so, figured "artists" list should list of images should either numpy arrays or png image or called pil (which not familiar with), , dont know how convert collection 1 of without saving image png or other format.
actually want do: a dynamic animation, when seek utilize im = plt.imshow(f(x, y)) 1 of drawings have, gives error:
typeerror: image info can not convert float i hope clear enough, first time animation , plotting tools. have solution?
here dynamic animation (that works in ipython notebook if want see way). essentially, want utilize draw_networkx , provide items plotted each frame. prevent positions changing on every phone call function, want reuse same positions (pos below).
%pylab inline #ignore out of ipython notebook ipython.display import clear_output #ignore out of ipython notebook import networkx nx graph= nx.complete_graph(5) #a simple graph 5 nodes f, ax = plt.subplots() pos=nx.spring_layout(graph) in range(5): nx.draw_networkx(graph, {j:pos[j] 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 finding max/mins time.sleep(0.05) clear_output(true) #for ipython notebook display(f) ax.cla() # turn off if you'd "build up" plots plt.close() python animation numpy matplotlib networkx
No comments:
Post a Comment