[weighted-degree]annual_distributor.py 2.51 KB
#-*-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_data/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

movie_year=input("영화의 개봉연도 4자리를 입력해주세요: ")
range_count=0



for i in range(7,507,1):
    movie_year_check=str(load_ws['C'+str(i)].value)
    distributor_name='L'+str(i)
    director_name='K'+str(i)
    if movie_year == movie_year_check[:4]:
        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)
        range_count+=1




for i in range(0, range_count):
    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, range_count):
    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 = movie_audience_list[i]/(max_audience+1))
    movie_graph_list.append(movie_graph)


movie_total = nx.Graph()

for i in range(0,range_count):
    movie_total = nx.compose(movie_total, movie_graph_list[i])

degree = movie_total.degree()
nx.write_gexf(movie_total, "Gephi/[weighted-degree]annual_distributor_"+movie_year+".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()