- 获得k-hop子图
subgraph = nx.ego_graph(G,node,radius=k)
neighbors= list(subgraph.nodes())
- 可视化并对目标节点高亮
# generate node positions:
pos = nx.spring_layout(ego_net)
# draw graph
nx.draw_networkx(ego_net, pos=pos, font_size=16, node_color='blue', font_color='white', with_labels=False) # with_labels控制是否显示节点的名称
# draw subgraph for highlights
nx.draw_networkx(ego_net.subgraph(node_idx), pos=pos, font_size=16, node_color='red', font_color='green', with_labels=False)
plt.savefig(os.path.join(savePath, f"ego_net_{node_idx.item()}.png"), format="PNG") # save fig
标签:node,color,pos,用法,nx,例子,ego,Networkx,font
From: https://www.cnblogs.com/mercurysun/p/16964185.html