【问题标题】:Very Slow network graph animation in Python's matplotlib with networkxPython的matplotlib中带有networkx的非常慢的网络图动画
【发布时间】:2017-12-07 05:11:11
【问题描述】:

我很想为随着时间的推移而增长的 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()

【问题讨论】:

  • 抱歉 - 当我复制并粘贴它时,我得到一个显示单个节点的图,然后是很多关键错误。
  • @Joel 这很奇怪,我将上面的代码复制到一个新的、独立的脚本中,它按预期运行。你有需要的进口吗?也许是更新的 networkx 和 matplotlib。另外,请粘贴最相关的错误消息,我也许能够找出您的问题是什么

标签: python animation matplotlib networkx digraphs


【解决方案1】:

我想我想通了。使用“matplotlib.patches import FancyArrowPatch, Circle”的函数正在减慢它的速度。这是“draw_network”函数。

我尝试用更简单的 (nx.draw(mainGraph, nodeToCoordinate_dict)) 替换绘图代码,并将时间间隔减少到 100(0.1 秒)。像魅力一样工作

现在,我只需要想出一种方法来加速曲线边缘的图形。

【讨论】:

  • 你能详细说明你是如何解决这个问题的吗?即使在 nx.draw_networkx_edges 中使用小图,花哨的箭头补丁也非常慢。你有什么更快的选择?
猜你喜欢
  • 2020-07-30
  • 2012-11-06
  • 2012-12-12
  • 1970-01-01
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
相关资源
最近更新 更多