import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_nodes_from([("01", {'size': 100}), ("02", {'size': 200}), ("03", {'size': 300})])
#G.add_edges_from([('01','02',{'weight':0.8}), ('02','03',{'weight':0.9}), ('01','03',{'weight':0.5})])
G.add_weighted_edges_from([('01','02',0.8), ('02','03',0.9), ('01','03',0.5)])
node_i = [v for v in G.nodes()]
nodeSize = [G.nodes[i]['size'] for i in node_i]#点权
edge_i = [v for v in G.edges()]
edgeWeig = [G.edges[i]['weight'] for i in edge_i]
print(nodeSize)
print(edgeWeig)
输出结果:
[100, 200, 300]
[0.8, 0.5, 0.9]
# 边权顺序发生变化 说明系统有相关储存边的方式
下面两个语句添加边权效果相同:
- G.add_edges_from([(’01’,’02’,{‘weight’:0.8}), (’02’,’03’,{‘weight’:0.9}), (’01’,’03’,{‘weight’:0.5})])
- G.add_weighted_edges_from([(’01’,’02’,0.8), (’02’,’03’,0.9), (’01’,’03’,0.5)])
nx.draw(G, with_labels=True, font_weight='bold', node_size = nodeSize, width = edgeWeig)
plt.show()
版权声明:本文为weixin_47710774原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。