Networkx边缘权重

时间:2017-05-15 15:31:06

标签: python-3.x networkx edges

我希望连接节点的边有不同的宽度,具体取决于我提供的具体值。此外,我正在尝试打印这些值(可能在相应的边缘之上)。

如果我只是在边缘权重中指示我的值,则不是所有边缘的宽度都被修改,而只是更接近我的节点的部分。而且,那是我打印价值的地方,而我似乎无法找到将其移到别处的方法。

这是我的代码:

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

G = nx.DiGraph()

G.add_edges_from([('1','2')],color = 'dimgray',weight=float(47.87))
G.add_edges_from([('3','2')],color = 'dimgray',weight=float(46.24))
G.add_edges_from([('3','1')],color = 'dimgray',weight=float(53.47))


labels={}
labels['2']=r'$2$'
labels['1']=r'$1$'
labels['3']=r'$3$'


pos = nx.circular_layout(G)

edges = G.edges()
colors = [G[u][v]['color'] for u,v in edges]
weights = [G[u][v]['weight'] for u,v in edges]
width= []
for i in weights:
    i = i/10
    width.append(i)
edge_labels=dict([((u,v,),d['weight'])
             for u,v,d in G.edges(data=True)])

nx.draw_networkx_labels(G,pos,labels,font_size=16)
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels,label_pos=0.3,font_size=15)

nx.draw(G, pos,node_size = 1000, edges=edges, edge_color=colors,
        node_color='lightgrey', width=width)
plt.show()

感谢您的帮助!

0 个答案:

没有答案