使用python绘制环形拓扑

问题描述

我需要绘制一个类似于下图所示的拓扑。但是我无法安排节点的顺序以及如何阻止边缘随机交叉。

Image

解决方法

您可以像这样对位置进行硬编码:

G = nx.DiGraph()

G.add_node(0,pos=(20,20))
G.add_node(1,pos=(30,10))
G.add_node(2,pos=(25,0))
G.add_node(3,pos=(15,0))
G.add_node(4,pos=(10,10))
pos=nx.get_node_attributes(G,'pos')

G.add_edge(0,1)
G.add_edge(1,2)
G.add_edge(2,3)
G.add_edge(3,4)
G.add_edge(4,0)

nx.draw(G,pos=pos,with_labels=True,font_weight='bold',node_size=1000,node_color = 'black',font_color = 'white')
plt.show()

结果:

enter image description here