简体   繁体   English

networkx / igraph上指定的边长(Python)

[英]Specified edge lengths on networkx/igraph (Python)

I wanted to visualize a network with the data I have and would like to graph them with specific edge lengths. 我希望用我拥有的数据可视化网络,并希望用特定的边长来绘制它们。 I use Python, and I've tried networkx and igraph to plot but all seem to assign fixed edge lengths. 我使用Python,我已经尝试使用networkx和igraph进行绘图,但所有似乎都分配了固定的边长。

a.) I wonder if I did the codes wrong or the packages aren't really capable. a。)我想知道我的代码是错误的还是包裹不是真的有能力。 How do you properly implement specified edge lengths for networkx or igraph? 如何正确实现networkx或igraph的指定边长?

b.) If networkx and igraph can't do it, what package could you possibly suggest? b。)如果networkx和igraph无法做到,您可以建议哪个包? (Preferably one that can carry over 80 thousand nodes.) (最好能携带超过8万个节点。)

Thanks! 谢谢!

This should work: 这应该工作:

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')

Most graph drawing algorithms use some version of SMACOF, which of course varies the edge length; 大多数图形绘制算法使用某种版本的SMACOF,这当然会改变边长; however, the graphviz layout engine 'neato' (supplied as the 2nd argument to 'draw' above) ought to preserve, if at all possible, user-set edge lengths. 但是,如果可能的话,graphviz布局引擎'neato'(作为上面'draw'的第二个参数提供)应该保留用户设置的边长。

The library i used here is certainly sturdy enough to handle 80,000 nodes. 我在这里使用的库肯定足以处理80,000个节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM