37,720
社区成员
发帖
与我相关
我的任务
分享
import networkx as nx
import matplotlib.pyplot as plt
from numpy import *
from matplotlib import animation
adjacent = []
edges = []
trM = []
v = []
node_size = []
def getNetwork():
input = open("adjacent.txt", "r")
for line in input.readlines():
one = []
line = line.strip().split(" ")
for x in range(len(line)):
one.append(int(line[x]))
adjacent.append(one)
input.close()
return adjacent
def adjacent2csv():
for i in range(len(adjacent)):
for j in range(len(adjacent[i])):
if adjacent[i][j] == 1:
edges.append((i+1, j+1))
return edges
def getTrMatrix():
a = array(adjacent)
b = transpose(a)
c = zeros((a.shape),dtype = float)
for i in range(a.shape[0]):
for j in range(a.shape[1]):
c[i][j] = a[i][j]/(b[j].sum())
return c
def init():
for i in range(len(adjacent[0])):
v[i] = float(1)/len(adjacent[0])
# nx.draw(G, pos=nx.shell_layout(G), node_color='w', node_size=[x * 40000 for x in v], with_labels = True)
return v
def animate(i):
global v
v = p*dot(trM,v) + (1-p)*v
nx.draw(G, pos=nx.shell_layout(G), node_color='w', node_size=[x * 40000 for x in v], with_labels = True)
return v
getNetwork()
adjacent2csv()
G = nx.DiGraph()
G.add_edges_from(edges)
layout = nx.spring_layout(G)
fig = plt.figure(figsize=(10, 10))
trM = getTrMatrix()
v = zeros((len(adjacent[0]),1),dtype = float)
p = 0.8
ani = animation.FuncAnimation(fig=fig, func=animate, frames=1000, init_func=init, interval=1000, repeat=False, blit=True)
plt.show()