Drawing multiple edges between two nodes with networkx

后端 未结 4 682
闹比i
闹比i 2020-12-05 20:31

I need to draw a directed graph with more than one edge (with different weights) between two nodes. That is, I have nodes A and B and edges (A,B) with length=2 and (B,A) wit

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 21:03

    An improvement to the reply above is adding the connectionstyle to nx.draw, this allows to see two parallel lines in the plot:

    import networkx as nx
    import matplotlib.pyplot as plt
    G = nx.DiGraph() #or G = nx.MultiDiGraph()
    G.add_node('A')
    G.add_node('B')
    G.add_edge('A', 'B', length = 2)
    G.add_edge('B', 'A', length = 3)
    
    pos = nx.spring_layout(G)
    nx.draw(G, pos, with_labels=True, connectionstyle='arc3, rad = 0.1')
    edge_labels=dict([((u,v,),d['length'])
                 for u,v,d in G.edges(data=True)])
    
    plt.show()
    

    See here the result

提交回复
热议问题