I'd like to write a program to animate fixation process in special network. During the animation, in each frame the color of nodes must be change by a special function. I wrote the following code, but it draw the network in each frame again that it's not my favor. I need a fixed network that just the color of nodes changes in each frame. But I don't know how do I code it!!! Is anybody knows how do I fixed it?
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt; plt.close('all')
import matplotlib.animation as animation
G = nx.complete_graph(10)
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
def animate(frame):
fig.clear()
colors_of_nodes = CON[frame, 0:N] # CON is a matrix that create during a
#process that I create in another code
nx.draw(G, node_color = colors_of_nodes, with_labels=True)
return colors_of_nodes
fig.set_tight_layout(False)
anim = animation.FuncAnimation(fig, animate, frames = rows, interval=500,
repeat=False, blit = True)
plt.show()
I changed something as a comment that I get, and now the network fixed, but the animate function doesn't work :( . This is the new code:
fig = plt.figure(figsize=(6,6))
pos = nx.spring_layout(G)
colors_of_nodes = 'blue'
nodes = nx.draw_networkx_nodes(G,pos, node_color =
colors_of_nodes)
edges = nx.draw_networkx_edges(G, pos)
labels = nx.draw_networkx_labels(G, pos)
def animate(frame):
colors_of_nodes = CON[frame, 0:N]
nodes.set_array(colors_of_nodes)
return nodes
anim = animation.FuncAnimation(fig, animate, frames = rows,
interval=500, repeat = False)
plt.show()
How can I able to solved the problem?