python networkx图论工具包如何画出edge的属性信息标签

networkx是一款非常好用的python下的图论分析工具,关于它的安装和如何构件图已经有很多大牛讲得很清楚里,但是我发现大家都没有提如何为画出来的图像中的edge或node在显示的过程中展示出其属性,在有的图中,展示属性有助于我们对这幅图有更清晰的认识,所以这里我将会向大家介绍如何为一幅图添加其node与edge属性。


import networkx as nx;
import matplotlib.pyplot as plt;

#create the graph
G = nx.Graph();
G.add_edge(0,41,band=42,cost=6);

pos=nx.spring_layout(G);
nx.draw_spring(G);
nx.draw_networkx_edge_labels(G,pos,font_size=10,alpha=0.5,rotate=True);

plt.show();

通过 pos = nx.spring_layout(G);便可以计算G中各个边的postion信息,而后在draw_network_edge_labels中就要在第二个参数中加载它的position信息,这里,python支持诸如shell_layout,circurt_layout,spring_layout,random_layout等多种形式,只需要在console中help nx.layout便可以查看多种分布方式,当然了,想要修改标签的属性也只需要在draw_networkx_edge_labels()的括号中赋予相应的参数,具体的参数信息也可以通过help 来得到。

画node的方法同理。

have fun!!!        :P

猜你喜欢

转载自blog.csdn.net/u013576018/article/details/60871485