How to increase node spacing for networkx.spring_layout

前端 未结 3 820
滥情空心
滥情空心 2020-12-02 20:29

Drawing a clique graph with

import networkx as nx
....
nx.draw(G, layout=nx.spring_layout(G))

produces the following picture:

3条回答
  •  情歌与酒
    2020-12-02 20:55

    I used the optimal distance parameter of the Kamada Kawai layout, and set the distance between non-connected components to the maximum distance in the graph. There is probably a better way of munging the dictionaries, but this is pretty easy:

    df = pd.DataFrame(index=G.nodes(), columns=G.nodes())
    for row, data in nx.shortest_path_length(G):
        for col, dist in data.items():
            df.loc[row,col] = dist
    
    df = df.fillna(df.max().max())
    
    layout = nx.kamada_kawai_layout(G, dist=df.to_dict())
    

提交回复
热议问题