Python中使用networkx库绘制带权图(以边的权值为宽度)

it2022-05-05  234

Python中使用networkx库绘制带权图(以边的权值为宽度)

#!-*- coding:utf8-*- import networkx as nx import matplotlib.pyplot as plt import random G=nx.Graph() for u, v in nx.barabasi_albert_graph(10,2,seed=1).edges(): G.add_edge(u,v,weight=random.uniform(0,0.4)) pos=nx.spring_layout(G,iterations=20) #以下语句绘制以带宽为线的宽度的图 nx.draw_networkx_edges(G,pos,width=[float(d['weight']*10) for (u,v,d) in G.edges(data=True)]) nx.draw_networkx_nodes(G,pos) plt.show()

其中d[‘weight’]*10中的10表示放大了权值的10倍,如果权值比较大,也可以进行缩小。


最新回复(0)