python网盘重量_Python Networkx重量标签定位

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()

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值