Improving Python NetworkX graph layout

前端 未结 4 1826
眼角桃花
眼角桃花 2020-12-07 11:07

I am having some problems in visualizing the graphs created with python-networkx, I want to able to reduce clutter and regulate the distance between the nodes (I have also t

4条回答
  •  抹茶落季
    2020-12-07 12:08

    You have a lot of data in your graph, so it is going to be hard to remove clutter.

    I suggest you to use any standard layout. You said that you used spring_layout. I suggest you to try it again but this time using the weight attribute when adding the edges.

    For example:

    import networkx as nx
    
    G = nx.Graph();
    G.add_node('A')
    G.add_node('B')
    G.add_node('C')
    G.add_node('D')
    G.add_edge('A','B',weight=1)
    G.add_edge('C','B',weight=1)
    G.add_edge('B','D',weight=30)
    
    pos = nx.spring_layout(G,scale=2)
    
    nx.draw(G,pos,font_size=8)
    plt.show()
    

    Additionally you can use the parameter scale to increase the global distance between the nodes.

提交回复
热议问题