假设我['a','b','c']
在网络中有节点,并且这些对存储在列表中:
[('a','b'), ('a','b'), ('b','a'), ('b','c'), ('a','c')]
我想使用NetworkX和matplotlib创建一个加权网络图。由于对('a','b')出现3次(在无向网络中,('b','a')也算在内),而('b','c')和('a' ,'c')仅出现1次,我想根据其重量更改边缘的宽度。
有人能对此有所启发吗?
这样的事情应该起作用。找出边是否存在以及是否会更新权重
default_weight = W
G = nx.Graph()
for nodes in node_list:
n0 = nodes[0]
n1 = nodes[1]
if G.has_edge(n0,n1):
G[n0][n1]['weight'] += default_weight
else:
G.add_edge(n0,n1, weight=default_weight)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句