我正在嘗試製作網絡圖。 它基本上是一個具有相應顏色的特定節點的第一個和第二個鄰居的子網。我已經包含了我的函數來創建第一和第二個顏色區域,並從這些列表中創建subgraph
。最近的鄰居的大網絡x圖圖
我認爲顏色部分正在工作,但由於佈局太小,我無法確定。我試過擴展spring_layout(G)
w/How to increase node spacing for networkx.spring_layout,但它似乎沒有工作。
我該如何創建一個網絡圖,在那裏我可以實際可視化這個規模的節點及其連接? (〜1000個節點)我打算添加with_labels
,但爲了簡單起見,未在圖像中添加該圖標。
我的主要目標是給一個足夠大的圖像上色的根節點「c」(青色),第一個鄰居「b」(藍色)和第二個鄰居「g」(綠色)並看到連接。
我相信我已經提供了足夠的代碼,請讓我知道如果我需要添加更多,我會編輯。
import networkx as nx
from collections import defaultdict
def neighborhood(G,node_list):
#G is the main nx.Graph() object that has ALL the nodes (~7000 nodes)
D_node_neighborhood = defaultdict(list)
for node in node_list:
#Color 1st neighbors blue
for n1 in G.neighbors(node):
D_node_neighborhood[node].append((n1,"b"))
#Color 2nd neighbors green
for n2 in G.neighbors(n1):
D_node_neighborhood[node].append((n2,"g"))
return(D_node_neighborhood)
def subnetwork(G,D_node_neighborhood,root_color = "c"):
D_node_subgraph = {}
for node,nghbr_color in D_node_neighborhood.items():
neighbors = [entry[0] for entry in nghbr_color]
colors = [entry[1] for entry in nghbr_color]
H = G.subgraph(neighbors + [node]) #Add root note to neighbors list and create subgraph
D_node_subgraph[node] = (H,colors + [root_color]) #Do the same with the colors
return(D_node_subgraph)
D_node_neighborhood.keys()[0] #Grab first one, this object is a list of tuples ("node-name","color-of-node") around 1000
G,colors = D_node_subgraph[node] #Separate them out
nx.draw(G,node_color=colors,node_size=10,alpha=0.8) #Draw the graph w/ corresponding colors
或者你可以去Gephi由'nx.write_gexf(G 'your_graph.gexf')' –