Specified edge lengths on networkx/igraph (Python)

后端 未结 1 1122
挽巷
挽巷 2020-12-30 09:44

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 a

1条回答
  •  一整个雨季
    2020-12-30 10:20

    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; 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.

    The library i used here is certainly sturdy enough to handle 80,000 nodes.

    0 讨论(0)
提交回复
热议问题