1、
#从excel中读入邻接矩阵,space_l和space_p
import numpy as np
import xlrd
def excel2m(path):#读excel数据转为矩阵函数
data = xlrd.open_workbook(path)
table = data.sheets()[0]
nrows = table.nrows # 行数
ncols = table.ncols # 列数
datamatrix = np.zeros((nrows, ncols))
for x in range(ncols):
cols = table.col_values(x)
cols1 = np.matrix(cols) # 把list转换为矩阵进行矩阵操作
datamatrix[:, x] = cols1 # 把数据进行存储
return datamatrix
ml = excel2m("gongjiao_l.xlsx")
mp = excel2m("gongjiao_p.xlsx")
2、
#从矩阵形成图
import matplotlib.pyplot as plt
import networkx as nx
G = nx.Graph()
for i in range(len(ml)):
for j in range(len(ml)):
if ml[i,j]==1:
G.add_edge(i, j)