Drawing multiple edges between two nodes with networkx

后端 未结 4 692
闹比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 20:45

    You can use matplotlib directly using the node positions you have calculated.

    G=nx.MultiGraph ([(1,2),(1,2),(1,2),(3,1),(3,2)])
    pos = nx.random_layout(G)
    nx.draw_networkx_nodes(G, pos, node_color = 'r', node_size = 100, alpha = 1)
    ax = plt.gca()
    for e in G.edges:
        ax.annotate("",
                    xy=pos[e[0]], xycoords='data',
                    xytext=pos[e[1]], textcoords='data',
                    arrowprops=dict(arrowstyle="->", color="0.5",
                                    shrinkA=5, shrinkB=5,
                                    patchA=None, patchB=None,
                                    connectionstyle="arc3,rad=rrr".replace('rrr',str(0.3*e[2])
                                    ),
                                    ),
                    )
    plt.axis('off')
    plt.show()
    

提交回复
热议问题