spring_layout
函数访问),而PyGraphviz有多个弹簧布局算法(可通过neato
程序和其他方式访问),将有向图布置为无向图。唯一真正处理图中方向的Graphviz/PyGraphviz布局程序是dot
,但dot
创建的是分层布局,而不是力导向布局。下面是一个示例,展示了有向图的弹簧布局在NetworkX和PyGraphviz之间的差异:
import networkx as nx
import pygraphviz as pgv
import matplotlib.pyplot as ppt
edgelist = [(1,2),(1,9),(3,2),(3,9),(4,5),(4,6),(4,9),(5,9),(7,8),(7,9)]
nxd = nx.DiGraph()
nxu = nx.Graph()
gvd = pgv.AGraph(directed=True)
gvu = pgv.AGraph()
nxd.add_edges_from(edgelist)
nxu.add_edges_from(edgelist)
gvd.add_edges_from(edgelist)
gvu.add_edges_from(edgelist)
pos1 = nx.spring_layout(nxd)
nx.draw_networkx(nxd,pos1)
ppt.savefig('1_networkx_directed.png')
ppt.clf()
pos2 = nx.spring_layout(nxu)
nx.draw_networkx(nxu,pos2)
ppt.savefig('2_networkx_undirected.png')
ppt.clf()
gvd.layout(prog='neato')
gvd.draw('3_pygraphviz_directed.png')
gvu.layout(prog='neato')
gvu.draw('4_pygraphviz_undirected.png')
1_networkx_directed.png:(http://farm9.staticflickr.com/8516/8521343506_0c5d62e013.jpg)
2_networkx_undirected.png:(http://farm9.staticflickr.com/8246/8521343490_06ba1ec8e7.jpg)
3_pygraphviz_directed.png:(http://farm9.staticflickr.com/8365/8520231171_ef7784d983.jpg)
4_pygraphviz_undirected.png:(http://farm9.staticflickr.com/8093/8520231231_80c7eab443.jpg)
The third and fourth images drawn are almost identical except for the arrowheads. Meanwhile, the first and second images differ in layout due to NetworkX's random element when using the spring_layout
. However, it is noticeable that regardless of randomness, the algorithm tries to place nodes closer to the center of the graph based on their connections. If a graph is directed, nodes with incoming arcs are often considered part of the periphery even if they contain many outgoing arcs. NetworkX's spring_layout
is both hierarchical and force-directed, which is helpful for analyzing core/periphery structures in directed graphs. Although most layout algorithms treat directed graphs as undirected, the different treatment of NetworkX is useful for analysis. Is it possible to replicate this using PyGraphviz / Graphviz?
这是使用PyGraphviz使用NetworkX的spring_layout函数绘制网络的结果(请参见下面我自己的答案)。 5_pygraphviz_plus_networkx.png: (http://farm9.staticflickr.com/8378/8520231183_e7dfe21ab4.jpg)
fdp
或sfdp
而不是neato
)并没有解决我的问题(正如您在写下“fdp布局将网络视为无向图”时所承认的那样),并且似乎没有Graphviz布局参数可以产生我想要的效果。我将在上面包含屏幕截图,以便您了解我的意思。(如果NetworkX文档解释了正在发生的事情,那肯定会有所帮助!) - Westcroft_to_Apsespring_layout
是否类似于那些“‘磁性’弹簧布局”,它也试图使弧线对齐,以传达层次结构。从我上面链接的图片来看,它似乎并不是试图让弧线指向相同的方向,而是试图让它们指向图形的中心。 - Westcroft_to_Apse