Networkx Spring Layout with Different Edge Values

后端 未结 2 1781
旧巷少年郎
旧巷少年郎 2021-01-03 09:24

I am new to Networkx and trying to figure out how to use the spring layout but applying different edge values (i.e., different distances between nodes) between nodes rather

2条回答
  •  一个人的身影
    2021-01-03 10:22

    According to the documentation spring_layout takes a weight-keyword which is the name of the edge attribute to use as weight when applying the layout. An example:

    import networkx as nx
    import random
    G = nx.path_graph(5)
    
    # Add some random weights (as dictonary with edges as key and weight as value).
    nx.set_edge_attributes(G, 'my_weight', dict(zip(G.edges(), [random.random()*10 for edge in G.edges()])))
    
    # Apply layout using weights.
    pos = nx.spring_layout(G, weight='my_weight')
    nx.draw(G, pos)
    

提交回复
热议问题