networkx 社交网络分析:绘制子图

时间:2021-05-12 01:38:49

标签: python pandas data-science networkx social-networking

使用 networkx,我有一个包含 20 个具有 eignvector 中心性的节点的列表。我想制作一个社交网络分析的子图,其中边和节点连接到这些前 20 个节点。

sorted_eigenvector = sorted(eigenvector_dict.items(), key=itemgetter(1), reverse=True)

print("Top 20 nodes by eigenvector centrality:")
for b in degree[:20]:
    print(b)

返回

特征向量中心性的前 20 个节点: ('shafiur', 0.45395678017648816) ('JusticeMyanmar', 0.3901833956501838) ('drzarni', 0.36392376672758797) ('Reaproy', 0.2588852510372045) ('maksimbenenson', 0.23262562211460833) ('SAdamsR2P', 0.1951118665108976) ('uscb', 0.19136049095052945) ('meemeeyeemon', 0.18760911539015862) ('本尼迪克特人',0.18010636426941695) ('KoSoe_T', 0.17635498870904612) ('forum_asia', 0.17635498870904612) ('hninyadanazaw', 0.1763549887090444) ('MayWongCNA', 0.16134948646756175) ('UNCANews', 0.14634398422607858) ('akilaGJC', 0.14634398422607858) ('loucharbon', 0.14634398422607858) ('GCR2P', 0.13884123310533691) ('mahninpwint', 0.10507885306200064) ('LeongWaiKitCNA', 0.09007335082051672) ('Milktea_Myanmar', 0.08257059969977519)

H = G.subgraph('shafiur')
H_neighbors = ["shafiur"]

for edge in G.edges():
  
  if edge[0]=="shafiur":
    H_neighbors.append(edge[1])


  elif edge[1]=="shafiur":
    H_neighbors.append(edge[0])
    
sub_graph = G.subgraph(H_neighbors)


edge_weights = [sub_graph.edges[v, w]["weight"] for v, w in sub_graph.edges]
plt.figure(figsize=(20,10))
pos = nx.spring_layout(sub_graph, k=2)
nx.draw(sub_graph, pos=pos, node_size=20, node_color="#73000A", edge_color=edge_weights, with_labels=True)
node_0_position = pos["shafiur"]
plt.plot(node_0_position[0], node_0_position[1], 'go', markersize=51 )

返回一个图,其中所有边和节点都连接到“shafiur”(sorted_eigenvector 中的第一个节点)。我想制作一个社交网络图,其中所有边和节点都连接出现在 sorted_eigenvector[:20] 中的所有节点,而不仅仅是 'shafiur'

1 个答案:

答案 0 :(得分:0)

只需获取所有“特殊”节点的邻居,然后归纳出子图:

import networkx as nx

g = nx.karate_club_graph()

special_nodes = [0, 5, 22]

neighbors = []
for node in special_nodes:
    neighbors.extend(g.neighbors(node))

h = g.subgraph(special_nodes + neighbors)

pos = nx.spring_layout(g)

fig, axes = plt.subplots(1, 2, sharex=True, sharey=True)
nx.draw(g, pos, with_labels=True, ax=axes[0])
nx.draw(h, pos, with_labels=True, ax=axes[1])
plt.show()

enter image description here

相关问题