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)