Drawing multiple edges between two nodes with networkx

后端 未结 4 685
闹比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:06

    Add the following code to AMangipinto's solution to add edge labels in both directions (see link for picture):

    edge_labels = dict([((u, v,), f'{d["length"]}\n\n{G.edges[(v,u)]["length"]}')
                    for u, v, d in G.edges(data=True) if pos[u][0] > pos[v][0]])
    
    nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_color='red')
    

    The "if pos[u][0] > pos[v][0]" only adds an edge label in one direction. We add both lengths to the single label otherwise we would over write the first label on an edge. Note: The label won't show if the nodes have the same x position.

    plot with edge labels

提交回复
热议问题