[Python] How can I draw graphs network using networkx and asking if there should be connection...

Discussão em 'Python' iniciado por Stack, Junho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am completely new on making network graphs in Python. I came to learn that networkx library can be used to plot network graphs in Python.

    I have 11 nodes. Node1, Node2, ..., Node11. I want to plot them in four different levels. Node1 is in level 1. Node2 and Node3 are in level 2. Node4 to Node7 are in level3, and Node8 to Node11 are in level 4.

    I have created a very simple basic network graph between level 1 and 2 using

    # Build a dataframe with 4 connections
    df = pd.DataFrame({ 'from':['Node1',"Node1"], 'to':["Node2","Node3"]})

    # Build your graph
    G=nx.from_pandas_edgelist(df, 'from', 'to')

    # Plot it
    nx.draw(G, arrows = True, with_labels = True)
    plt.show()


    And it looks as follows:[​IMG] I want to plot Node2 and Node3 in the same direction towards right of Node1 above and below it respectively. Is it also possible to get a rectangle instead of a circle in the node, and is it also possible to display the labels outside of the node?

    To plot between level 2 and level3, I want to ask the user if there is edgelink between each of Node2 and Node3, with each of Node4, Node5, Node6 and Node7. Also, I want to ask the user if there is edge link between each node in level 3 and level 4 respectively?

    It should look something as shown below and the connections between level 2 and level 3, and level 3 and level 4 should be based on user input. What would be the suitable way to obtain this network graph plot? Is there any other libraries better suited for this?[​IMG]

    Continue reading...

Compartilhe esta Página