> 文档中心 > 【python+networkx】

【python+networkx】

import networkx as nximport matplotlib.pyplot as pltimport mathG=nx.Graph()# G=nx.DiGraph()#有向图# G=nx.MultiGraph()# G=nx.MultiDiGraph()G.add_edge(1,2)G.add_edge(2,3,weight=0.9)G.add_edge('y','x',function=math.cos)G.add_node(math.cos)#图elist=[(1,2),(2,3),(1,4),(4,2)]G.add_edges_from(elist)#加有权重的图elist2=[('a','b',5.0),('b','c',3.0),('a','c',1.0),('c','b',7.3)]G.add_weighted_edges_from(elist2)# 随机节点G.add_node(1)G.add_nodes_from([2,3])G.add_nodes_from(range(100,110))H=nx.path_graph(10)G.add_nodes_from(H)#cubical_graph返回3-正则柏拉图立体图G=nx.cubical_graph()# subplot启到平铺图像作用# subplot(1,2,1)是指一个1行2列的图中从左到右从上到下的第一个位置subax1=plt.subplot(121)nx.draw(G)# subplot(1,2,2)是指一个1行2列的图中从左到右从上到下的第二个位置subax2=plt.subplot(122)nx.draw(G,pos=nx.circular_layout(G),node_color='r',edge_color='b')nx.draw(G)plt.show()

在这里插入图片描述