[Python] Interaction between networkx and matplotlib

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying networkx and visualization in matplotlib an I'm confused becouse I do not clearly understand how do they interact with each other? There simple example

    import matplotlib.pyplot
    import networkx as nx
    G=nx.path_graph(8)
    nx.draw(G)
    matplotlib.pyplot.show()


    Where do I tell pyplot, that I want to draw graph G? I guess that nx.draw use something like matplotlib.pyplot.{plot, etc ...} So, if I want to draw 2 graphs:

    import matplotlib.pyplot
    import networkx as nx

    G=nx.path_graph(8)
    E=nx.path_graph(30)
    nx.draw(G)

    matplotlib.pyplot.figure()
    nx.draw(E)
    matplotlib.pyplot.show()


    Then... little experiment

    import networkx as nx
    G=nx.path_graph(8)
    E=nx.path_graph(30)
    nx.draw(G)
    import matplotlib.pyplot
    matplotlib.pyplot.figure()
    nx.draw(E)
    import matplotlib.pyplot as plt
    plt.show()


    Please don't kill me about this stupid code, I am just trying to understand - how do networkx draw something matplotlib, while it even doesn't import yet!

    P.S: Sorry for my English.

    Continue reading...

Compartilhe esta Página