Python 20190208读入txt并做网络关系图(1)

1.主要库 networkx 与 pyplot
2. 注意检查文本文档不要有多余空格,使用strip删除行尾回车,使用split进行列表划分
3. 使用addedge添加边,目前尚无增加节点与边的标签
import networkx
import matplotlib.pyplot as plt
df1 = open(‘dianqi_edge.txt’,‘r’)
ncols1 = len(next(df1).split(’\n’))
g = networkx.Graph()
edge_lable=[]
for eline in df1:
eline = eline.strip(’\n’’)
earray = eline.split(’ ')
g.add_edge(earray[0],earray[1])
edge_lable.append(earray[2])
df1.close()
networkx.draw(g)
plt.show()
Python 20190208读入txt并做网络关系图(1)_第1张图片

你可能感兴趣的:(Python 20190208读入txt并做网络关系图(1))