Search code examples
pythongraphnetworkxgraphviz

Generate Network plot from dataframe


Suppose I have this dataframe where column a-number represents nodes with directed edges to nodes in b-number:

    a_number    b_number
0   343              991
1   991              633
2   343              633
3   633              628
4   343              633
5   628              916
6   697              886
7   916              572
8   697              884
9   886              125

How can I generate an image representation of this graph that looks like this: enter image description here


Solution

  • Networkx is the go-to library for graphs in python: https://networkx.org/documentation/stable/index.html

    First do the import:

    import networkx as nx
    

    To start a graph like that declare a inicialize DiGraph (directed graph):

    G = nx.DiGraph()
    

    Then add some nodes:

    G.add_node(343)
    G.add_node(991)
    G.add_node(633)
    

    Then some edges:

    G.add_edge(343,991)
    G.add_edge(991,633)
    G.add_edge(343,633)
    

    Finaly draw the graph G:

    nx.draw(G, with_labels = True, font_size=14 , node_size=2000)
    

    use the with_labels = True so you can have the node numbers, node_size=2000 to make the nodes bigger and font_size=14 to make the font also bigger

    This is the output of the code:

    This is the output of the code

    Now to read from the dataframe, just do a cycle like:

    for  i, (x, y) in df.iterrows():
        G.add_node(x)
        G.add_node(y)
        G.add_edge(x,y)
    

    If the nodes or edges already exists it will not add a new one, so you don't need to worry about it