Remove rotation effect when drawing a square grid of MxM nodes in networkx using grid_2d_graph

前端 未结 2 2053
伪装坚强ぢ
伪装坚强ぢ 2021-01-02 17:35

I need to generate a regular graph (also known as lattice network) which has 100x100 nodes. I started off with drawing a 10x10 graph with the follo

2条回答
  •  臣服心动
    2021-01-02 18:21

    By default, networkx.draw uses a spring layout. Instead, you can provide your own positions with parameter pos. This is actually really simple, since the labels of nodes given networkx.grid_2d_graph actually are a (row, column) tuple:

    >>> G=nx.grid_2d_graph(2,2)
    [(0, 1), (1, 0), (0, 0), (1, 1)]
    

    Thus you can use a node's name as its position. So you just need to create a dictionary mapping nodes to themselves, and pass that as the position.

    pos = dict( (n, n) for n in G.nodes() )
    

    However, since you also want to add node labels, you should use networkx.draw_networkx, which takes a dictionary of custom labels as an optional parameter. You'll need a dictionary mapping nodes to their new labels. Since NetworkX gives each node the label (row, column) by default, we can just label each node with row * 10 + column:

    labels = dict( ((i, j), i * 10 + j) for i, j in G.nodes() )
    

    Putting it all together, you get the following code which yields the graph below:

    import networkx as nx
    import matplotlib.pyplot as plt
    
    N = 10
    G=nx.grid_2d_graph(N,N)
    pos = dict( (n, n) for n in G.nodes() )
    labels = dict( ((i, j), i * 10 + j) for i, j in G.nodes() )
    nx.draw_networkx(G, pos=pos, labels=labels)
    
    plt.axis('off')
    plt.show()
    

    EDIT

    Using the suggestion from @AbdallahSobehy, we can label the nodes from left to right and top to bottom.

    labels = dict( ((i, j), i + (N-1-j) * 10 ) for i, j in G.nodes() )
    

提交回复
热议问题