import networkx as nx
import matplotlib.pyplot as plt
1、对网络进行可视化(网络为两列的格式)
benchmark=open('network.txt')
benchmark=benchmark.read()
G = nx.Graph()
liebiao=benchmark.split("\n") 用回车符分裂字符串
for i in xrange(0,len(liebiao)-1):
liebiao1=liebiao[i].split() 用空格符分裂0和1
node1=int(liebiao1[0]) 节点转化为整数
node2=int(liebiao1[1])
G.add_edge(node1,node2)
pos = nx.spring_layout(G)
nx.draw(G,pos,with_labels=True, node_size =b)
plt.show()
2、对得到社区划分结果后的网络进行可视化-得到不同社区为不同颜色和节点形状的网络结构图
例如:membership_list=[[0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 16, 17, 19, 21],
[8, 9, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]]
pos=nx.spring_layout(G)
plt.figure(figsize = (20,16))
nx.draw(G,pos,with_labels