使用Networkx在图形中不显示边缘-Python

时间:2018-09-25 04:02:18

标签: python matplotlib graph networkx

我正在使用networkx和matplotlib在python中绘制有向图。但是,并非所有边缘都可见。这些节点彼此之间是如此接近,以至于两个节点之间都没有显示路径。

代码:

import networkx as nx

resultNodes = ['P0', 'P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9']

resultEdges = [('P0', 'P4'), ('P0', 'P2'), ('P0', 'P1'), ('P1', 'P7'), ('P1', 'P8'),
               ('P1', 'P2'), ('P2', 'P3'), ('P4', 'P5'), ('P4', 'P6'), ('P5', 'P6'),
               ('P9', 'P2'), ('P9', 'P7'), ('P9', 'P1')]

G=nx.DiGraph()

G.add_nodes_from(resultNodes)
G.add_edges_from(resultEdges)

print("Nodes of graph: ")
print(G.nodes())
print("Edges of graph: ")
print(G.edges())

nx.draw(G,with_labels=True,font_size=15,node_color='yellowgreen',node_size=1000)

Output image shows no edge between P4,P5,P6 as those nodes are so close

1 个答案:

答案 0 :(得分:1)

您可以计算节点的位置,然后传递给绘图函数。参见the doc

示例:

pos = nx.drawing.layout.circular_layout(G)

nx.draw(G, pos=pos, with_labels=True, font_size=15,
        node_color='yellowgreen', node_size=1000)

circular_layout