Python Networkx 权重标签定位 [英] Python Networkx Weight Labels Positioning

查看:19
本文介绍了Python Networkx 权重标签定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在图中产生了一个非常狡猾"的边缘权重标签放置.请看图片.我想要一个更好的位置(靠近每条线的中点),同时仍然利用节点的自动定位 - 即我不想手动定位节点.

有什么想法吗?还有一个警告 - iterable 函数在 Matplotlib 3.1 中被弃用,将在 3.3 中删除.改用 np.iterable. 如果有人知道如何解决这个问题会很好.

将 matplotlib.pyplot 导入为 plt将 networkx 导入为 nx将 numpy 导入为 npG = nx.Graph()G.add_nodes_from(["A", "B", "C"])G.add_edge("A", "B", weight=5)G.add_edge("B", "C", weight=7)G.add_edge("C", "A", weight=2)pos = nx.spring_layout(G)权重 = nx.get_edge_attributes(G,权重")nx.draw_networkx(G, with_labels=True)nx.draw_networkx_edge_labels(G, pos, edge_labels=weights)plt.show()

解决方案

来自 draw_networkx 的文档:

<块引用>

draw_networkx(G, pos=None, arrows=True, with_labels=True, **kwds)参数:[...]pos (dictionary, optional) – 一个以节点为键,位置为值的字典.如果未指定弹簧布局定位将被计算.有关计算节点的功能,请参阅 networkx.layout职位.

所以,如果你没有显式传递pos,就会生成一个spring_layout,但这与你通过生成的布局不同

pos = nx.spring_layout(G)

,因为调用 nx.spring_layout(G) 两次给出不同的结果:

 for a in [0,1]:pos = nx.spring_layout(G)打印(位置)

输出:

{'A': array([ 0.65679786, -0.91414348]), 'B': array([0.34320214, 0.5814527 ]), 'C': array([-1. , 0.3326])}9078{'A':数组([-0.85295569,-0.70179415]),'B':数组([0.58849111,-0.29820585]),'C':数组([0.26446458,1.])}

因此,将相同的 pos 传递给两个绘图函数即可解决问题:

pos = nx.spring_layout(G)权重 = nx.get_edge_attributes(G,权重")nx.draw_networkx(G, pos, with_labels=True)nx.draw_networkx_edge_labels(G, pos, edge_labels=weights)

The code below produces a very "dodgy" placement of the labels for edge weights in a graph. Please see image. I would like to have a better placement (close to the midpoint of each line), while still taking advantage of the automatic positioning of the nodes - i.e. I don't want to have to manually position the nodes.

Any ideas please? Also there is a warning - The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead. which would be good to address if anyone knows how.

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np

G = nx.Graph()
G.add_nodes_from(["A", "B", "C"])
G.add_edge("A", "B", weight=5)
G.add_edge("B", "C", weight=7)
G.add_edge("C", "A", weight=2)

pos = nx.spring_layout(G)
weights = nx.get_edge_attributes(G, "weight")
nx.draw_networkx(G, with_labels=True)
nx.draw_networkx_edge_labels(G, pos, edge_labels=weights)

plt.show()

解决方案

From the documentation of draw_networkx:

draw_networkx(G, pos=None, arrows=True, with_labels=True, **kwds)
Parameters:   
[...]
pos (dictionary, optional) – A dictionary with nodes as keys and positions as values. If not specified a spring layout positioning will
be computed. See networkx.layout for functions that compute node
positions.

So, if you do not pass pos explicitly, a spring_layout is generated, but this won't be identical to the layout that you generate through

pos = nx.spring_layout(G)

, because calling nx.spring_layout(G) twice gives different results:

for a in [0,1]:
    pos = nx.spring_layout(G)
    print(pos)

output:

{'A': array([ 0.65679786, -0.91414348]), 'B': array([0.34320214, 0.5814527 ]), 'C': array([-1.        ,  0.33269078])}
{'A': array([-0.85295569, -0.70179415]), 'B': array([ 0.58849111, -0.29820585]), 'C': array([0.26446458, 1.        ])}

So, passing the same pos to both drawing functions solves the problem:

pos = nx.spring_layout(G)
weights = nx.get_edge_attributes(G, "weight")
nx.draw_networkx(G, pos, with_labels=True)
nx.draw_networkx_edge_labels(G, pos, edge_labels=weights)

这篇关于Python Networkx 权重标签定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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