【问题标题】:How to show weights as part of the plot?如何将权重显示为情节的一部分?
【发布时间】:2021-12-02 06:47:20
【问题描述】:

如何将权重显示为图表的一部分?

my_graph = nx.Graph()
edges = nx.read_weighted_edgelist('edges.txt')
nodes = nx.read_adjlist("nodes.txt")
pos = nx.shell_layout(my_graph)
my_graph.add_edges_from(edges.edges())
my_graph.add_nodes_from(nodes)
nx.draw(my_graph, with_labels=True)
plt.show()
plt.close()

我尝试了另一种方法

labels = {e: g.edges[e]['weight'] for e in g.edges}
nx.draw_networkx_edge_labels(g,pos,edge_labels=labels)

但是对于这种情况,我得到了一个非常混乱的情节。

如何获得与第一个类似但显示权重的图?

【问题讨论】:

    标签: python nodes networkx


    【解决方案1】:

    使用pos = nx.spring_layout(my_graph) 而不是pos = nx.shell_layout(my_graph)

    节点的位置由布局命令决定。

    【讨论】:

    • 为了进一步澄清@matho_noob 的答案,发帖者问题中两个图的节点位置不同,因为第一个nx.draw() 命令实际上并没有通过pos。由于没有在第一次调用中指定,默认情况下使用nx.spring_layout(),根据docs for nx.draw()
    • 非常感谢!我现在知道了,它可以工作了!
    猜你喜欢
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    相关资源
    最近更新 更多