how to draw directed graphs using networkx in python?

后端 未结 6 765
灰色年华
灰色年华 2020-12-04 04:47

I have some nodes coming from a script that I want to map on to a graph. In the below, I want to use Arrow to go from A to D and probably have the edge colored too in (red o

6条回答
  •  误落风尘
    2020-12-04 05:33

    import networkx as nx
    import matplotlib.pyplot as plt
    
    g = nx.DiGraph()
    g.add_nodes_from([1,2,3,4,5])
    g.add_edge(1,2)
    g.add_edge(4,2)
    g.add_edge(3,5)
    g.add_edge(2,3)
    g.add_edge(5,4)
    
    nx.draw(g,with_labels=True)
    plt.draw()
    plt.show()
    

    This is just simple how to draw directed graph using python 3.x using networkx. just simple representation and can be modified and colored etc. See the generated graph here.

    Note: It's just a simple representation. Weighted Edges could be added like

    g.add_edges_from([(1,2),(2,5)], weight=2)
    

    and hence plotted again.

提交回复
热议问题