【发布时间】:2016-06-16 05:43:55
【问题描述】:
我正在尝试制作网络图。 它基本上是来自具有相应颜色的特定节点的第一个和第二个邻居的子网络。我已经包含了我的函数来创建带有颜色的第一个和第二个邻居以及从这些创建 subgraph列表。
我认为颜色部分可以正常工作,但由于布局太小,我无法确定。我尝试用How to increase node spacing for networkx.spring_layout 扩展spring_layout(G),但它似乎没有用。
我如何创建一个网络图,我可以在其中实际可视化节点及其如此大的连接? (~ 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
【问题讨论】:
标签: python matplotlib graph network-programming networkx