使用networkx在两个节点之间绘制多个边 [英] Drawing multiple edges between two nodes with networkx

查看:828
本文介绍了使用networkx在两个节点之间绘制多个边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要绘制一个有向图,其中两个节点之间有多个边缘(权重不同).也就是说,我有节点A和B,以及边(A,B)的长度= 2和(B,A)的长度= 3.

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) with length=3.

我已经尝试过同时使用G = nx.Digraph和G = nx.Multidigraph.绘制时,我只能查看一个边缘和一个标签. 有什么办法吗?

I have tried both using G=nx.Digraph and G=nx.Multidigraph. When I draw it, I only get to view one edge and only one of the labels. Is there any way to do it?

推荐答案

尝试以下操作:

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)
edge_labels=dict([((u,v,),d['length'])
             for u,v,d in G.edges(data=True)])
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, label_pos=0.3, font_size=7)
plt.show()

这将为您返回此图形,该图形具有两个边缘以及边缘上显示的长度:

This will return you this graph with two edges and the length shown on the edge:

这篇关于使用networkx在两个节点之间绘制多个边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆