Loading

Networkx用法例子总结

  1. 获得k-hop子图
subgraph = nx.ego_graph(G,node,radius=k)
neighbors= list(subgraph.nodes())
  1. 可视化并对目标节点高亮
# 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
posted @ 2022-12-07 18:38  摇头晃脑学知识  阅读(112)  评论(0编辑  收藏  举报