時間の経過とともに成長するnetworkxグラフをアニメーション化するのに苦労しています。私はそれを湾曲したエッジとすべてで動作させています。非常に満足していますが、グラフが大きくなるにつれて、かなり遅くなります!? より速く実行するために指定されたコードを説明または変更できる人はいますか。問題があるのは自分のコンピュータではないと思います。注: ファンシーな湾曲したエッジがなくても、非常に低速でした。単純な nx.draw でも同じ問題が発生します
import time
from matplotlib import pyplot as plt
from matplotlib import animation
import networkx as nx
from matplotlib.patches import FancyArrowPatch, Circle
import numpy as np
mainGraph = nx.DiGraph()
nodeToCoordinate_dict = {}
label_coordinatePositions = {}
edgeLabel_coordinatePositions = {}
edgeLabel_values_dict = {}
fig = plt.figure()
#=============================================================
def draw_network(G,pos,ax,sg=None):
e = None
for n in G:
c=Circle(pos[n],radius=0.02,alpha=0.5)
ax.add_patch(c)
G.node[n]['patch']=c
x,y=pos[n]
seen={}
for (u,v,d) in G.edges(data=True):
n1=G.node[u]['patch']
n2=G.node[v]['patch']
rad=0.1
if (u,v) in seen:
rad=seen.get((u,v))
rad=(rad+np.sign(rad)*0.1)*-1
alpha=0.5
color='k'
e = FancyArrowPatch(n1.center,n2.center,patchA=n1,patchB=n2,
arrowstyle='-|>',
connectionstyle='arc3,rad=%s'%rad,
mutation_scale=10.0,
lw=2,
alpha=alpha,
color=color)
seen[(u,v)]=rad
ax.add_patch(e)
return e
#========================================================
def animate(i):
startTime = time.time()
"""perform animation step"""
global mainGraph, nodeToCoordinate_dict, label_coordinatePositions
mainGraph.add_node(str(i%20)+","+str(i/20), pos = (i%20,i/20))
if not (i%20 == 0):
mainGraph.add_edge(str(i%20-1)+","+str(i/20), str(i%20)+","+str(i/20))
prevNode_coordinate = nodeToCoordinate_dict[str(i%20-1)+","+str(i/20)]
mainGraph[str(i%20-1)+","+str(i/20)][str(i%20)+","+str(i/20)]['p'] = 1.0
#END IF
nodeToCoordinate_dict[str(i%20)+","+str(i/20)] = (i%20,i/20)
ax=plt.gca()
draw_network(mainGraph,nodeToCoordinate_dict,ax)
ax.autoscale()
plt.axis('equal')
plt.axis('off')
print("time Elapsed = ", time.time()-startTime)
return None #mainGraph
#====================================================
ani = animation.FuncAnimation(fig, animate, frames= 60,interval=1000)
plt.show()