Search code examples
pythongraphnetworkx

NetworkX Python Diferrent output with same input


Why everytime that a script runs shows a different structure? Like, the logic it´s the same but it shows diferent order, is there a way to keep the same seed or structure? Examples of same data but different results: Example of the same data

Example of the same data


Solution

  • You can save the position of the nodes in a variable so that the network is represented always in the same way: First create the graph:

    G1 = nx.barabasi_albert_graph(20, 2)
    

    Then run the layout function:

    pos = nx.spring_layout(G1)
    

    Then draw the graph like this:

    nx.draw(G1, pos=pos)
    

    The nx.spring_layout and other layout functions also allow for a seed value:

    nx.spring_layout(G1, seed=31415)
    

    Please note that the order of the nodes in you graph may change each time you create your graph. This may be what is affecting the layout of the graph. Try to run each function separately.