python networkx 画图
#!/home/mengdan/anaconda2/bin/python# -*- coding: utf-8 -*-import networkx as nximport matplotlib.pyplot as plt# 构图edge_list = [(1,2,3),(2,3,2),(1,3,1),(2,4,2),(3,5,2),(2,6,1),(6,7,1),...
·
#!/home/mengdan/anaconda2/bin/python
# -*- coding: utf-8 -*-
import networkx as nx
import matplotlib.pyplot as plt
# 构图
edge_list = [(1,2,3),(2,3,2),(1,3,1),(2,4,2),(3,5,2),(2,6,1),(6,7,1),
(5,7,1),(5,8,1),(1,9,1),(9,10,2),(5,9,3),(8,9,2),(10,11,1)]
G = nx.Graph() #建立一个空的无向图G
# G.add_node(1) #添加一个节点1
# G.add_edge(2,3) #添加一条边2-3(隐含着添加了两个节点2、3)
# G.add_edges_from(edge_list) #adding a list of edges
G.add_weighted_edges_from(edge_list)
# 布局
pos=nx.spring_layout(G,iterations=20)
# circular_layout:节点在一个圆环上均匀分布
# random_layout:节点随机分布
# shell_layout:节点在同心圆上分布
# spring_layout: 用Fruchterman-Reingold算法排列节点(这个算法我不了解,样子类似多中心放射状)
# spectral_layout:根据图的拉普拉斯特征向量排列节
# 打印
print "nodes:", G.nodes() #输出全部的节点: [1,2, ...,10]
print "edges:", G.edges() #输出全部的边:[(1,2),..,(9,10)]
print "number of edges:", G.number_of_edges() #输出边的数量:11
#save & 展示
# nx.draw_networkx(G)
nx.draw_networkx_edges(G,pos,width=[float(d['weight']*2) for (u,v,d) in G.edges(data=True)])
nx.draw_networkx_nodes(G,pos)
nx.draw_networkx_labels(G,pos) #节点标号
plt.savefig("./visualization/undirected3.png")
plt.show()
更多推荐



所有评论(0)