【问题标题】:Edges don't show in a graph using Networkx - Python使用 Networkx - Python 的图形中不显示边
【发布时间】:2019-02-28 14:51:13
【问题描述】:

我正在使用 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)

【问题讨论】:

标签: python matplotlib graph networkx


【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-22
    • 2013-12-21
    • 2023-03-22
    • 2021-05-14
    • 2016-06-08
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    相关资源
    最近更新 更多