betweenness,closeness.py
2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#-*-encoding utf-8 -*-
import matplotlib.pyplot as plt
import networkx as nx
from openpyxl import load_workbook
movie_director_list = []
movie_audience_list = []
movie_edge_list = []
movie_graph_list = []
movie_check_list = []
max_audience=-1
load_wb = load_workbook("movie_top500.xlsx", data_only=True)
load_ws = load_wb['Sheet1']
def make_edge(movielist):
edge_list = []
for i in range(0,len(movielist)):
for j in range(0, len(movielist)):
if i == j:
break
else:
edge_list.append((movielist[i],movielist[j]))
return edge_list
for i in range(7,507,1):
distributor_name='L'+str(i)
director_name='K'+str(i)
name3=(str(load_ws[distributor_name].value) +','+ str(load_ws[director_name].value)).split(',')
movie_audience_list.append(int(load_ws['F'+str(i)].value))
movie_director_list.append(name3)
for i in range(0, 500):
if movie_director_list[i] in movie_check_list:
movie_audience_list[i]=movie_audience_list[i]+movie_audience_list[movie_check_list.index(movie_director_list[i])]
movie_audience_list[movie_check_list.index(movie_director_list[i])]=movie_audience_list[i]
movie_check_list.append(movie_director_list[i])
else:
movie_check_list.append(movie_director_list[i])
if movie_audience_list[i] > max_audience:
max_audience=movie_audience_list[i]
for i in range(0, 500):
movie_graph = nx.Graph()
movie_edge = make_edge(movie_director_list[i])
movie_edge_list.append(movie_edge)
movie_graph.add_nodes_from(movie_director_list[i])
movie_graph.add_edges_from(movie_edge_list[i], weight = 1-(movie_audience_list[i]/(max_audience+1)))
movie_graph_list.append(movie_graph)
movie_total = nx.Graph()
for i in range(0,500):
movie_total = nx.compose(movie_total, movie_graph_list[i])
degree = movie_total.degree()
nx.write_gexf(movie_total, "movie.gexf")
plt.figure(figsize=(20,20))
pos = nx.spring_layout(movie_total, iterations = 10)
nx.draw(movie_total, pos, alpha = 1, line_color= 'black', linewidths = 0, width = 0.05 ,node_size = 100, node_color = 'w', with_labels = True, font_family='NanumGothic', font_color='black', font_size=8)
plt.show()