-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathanalysis_graph.py
More file actions
59 lines (49 loc) · 1.92 KB
/
analysis_graph.py
File metadata and controls
59 lines (49 loc) · 1.92 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
import networkx as nx
import json
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
with open('./repo_graph.json', 'r') as file:
data = json.load(file)
G = nx.node_link_graph(data)
print("Number of nodes:", G.number_of_nodes())
print("Number of edges:", G.number_of_edges())
# 计算度中心性
degree_centrality = nx.degree_centrality(G)
# 找出度中心性最高的前N个节点
top_n = 10
top_nodes = sorted(degree_centrality.items(), key=lambda item: item[1], reverse=True)[:top_n]
print("Top {} nodes by degree centrality:".format(top_n))
for node, centrality in top_nodes:
print(f"Node: {node}, Degree Centrality: {centrality}")
# 聚类系数
clustering_coefficient = nx.clustering(G)
average_clustering_coefficient = nx.average_clustering(G)
print("Average Clustering Coefficient:", average_clustering_coefficient)
# # 计算平均路径长度
# if nx.is_connected(G):
# average_path_length = nx.average_shortest_path_length(G)
# print("Average Path Length:", average_path_length)
# else:
# print("The graph is not connected. Cannot compute average path length.")
#
# # 计算直径
# if average_path_length is not None:
# diameter = nx.diameter(G)
# print("Diameter:", diameter)
# else:
# print("Cannot compute diameter because the graph is not connected.")
# 布局图
pos = nx.spring_layout(G)
# 使用matplotlib绘制图
plt.figure(figsize=(12, 12))
nx.draw_networkx_nodes(G, pos, node_color='lightblue', edgecolors='k')
nx.draw_networkx_edges(G, pos, width=0.5, alpha=0.5, arrows=False)
nx.draw_networkx_labels(G, pos, font_size=8, font_family='sans-serif')
# 突出显示度中心性最高的节点
for node, centrality in top_nodes:
nx.draw_networkx_nodes(G, pos, node, node_color='red', node_size=500)
# 显示图
plt.title('Repository Collaboration Network')
plt.axis('off') # 关闭坐标轴
plt.savefig('./repo_graph.png')