10

我想用我拥有的数据来可视化一个网络,并想用特定的边长来绘制它们。我使用 Python,并且尝试使用 networkx 和 igraph 进行绘图,但似乎都指定了固定的边长。

a.) 我想知道是我做错了代码还是软件包没有真正的能力。您如何正确实现 networkx 或 igraph 的指定边长?

b.) 如果 networkx 和 igraph 做不到,你会建议什么包?(最好是可以承载超过 8 万个节点的。)

谢谢!

4

1 回答 1

8

这应该有效:

import networkx as NX
import pygraphviz as PG

G = PG.AGraph()
nlist = "A B C D E".split()
a, b = "A A B", "B C D"
elist = zip(a.split(), b.split())

G.add_nodes_from(nlist)
G.add_edges_from(elist)
G.node_attr.update(color="red", style="filled")
G.edge_attr.update(color="blue", len="2.0", width="2.0")

print(G.edge_attr)
# returns {'color': 'red', 'width': '', 'len': '2.0'}

# add new edge with custom length (all others have length=2.0):
G.add_edge("C", "E", len="3.0", color="blue", width="2.0")

edge = G.get_edge("C", "E")
print(edge_attr)
# returns {'color': 'blue', 'width': '2.0', 'len': '3.0'}

# and you can confirm that introspection by drawing & printing this graph:
G.draw('somefolderandfilename.png', format='png', prog='neato')

大多数图形绘制算法都使用某些版本的 SMACOF,这当然会改变边长;但是,graphviz 布局引擎“neato”(作为上面“draw”的第二个参数提供)应该尽可能保留用户设置的边长。

我在这里使用的库肯定足够强大,可以处理 80,000 个节点。

于 2009-12-14T01:09:11.633 回答