Networkx Subgraphs

45 views
Skip to first unread message

Matthew Kline

unread,
May 2, 2022, 11:22:02 AMMay 2
to networkx-discuss
I have created a function that allows me to search my network for a node that returns and creates a network comprised of that given node's connections.  My only issue is that my subgraphs are made through G.neighbors so all my edges are lost and I only have a disconnected network.  Does anyone know how to create subgraphs from a main graph that produces all connected nodes for a given node? My network is rather large so I cannot manually make these connections in the subgraph but instead maintain the relationships from my main network.

Thanks!

Dan Schult

unread,
May 2, 2022, 12:53:47 PMMay 2
to networkx...@googlegroups.com
Does `Gsub = G.subgraph(nodes_I_want_to_include)` work?
So for example:  `Gsub = G.subgraph(G.neighbors(node))`
There is also a function called `nx.ego_graph(G, node)` which does this sort of thing.

Matthew Kline

unread,
May 10, 2022, 9:16:07 AMMay 10
to networkx-discuss
Thank you so much for your reply.  The ego graph was perfect because it maintained edge relationships.  Do you know if there is a way to center a specific node in an ego graph? I want to visually show which node the graph is showing the relationships for. Thanks again!

Dan Schult

unread,
May 10, 2022, 9:41:29 AMMay 10
to networkx...@googlegroups.com
I have used the `shell_layout` function for visualizing ego graphs.

Matthew Kline

unread,
May 10, 2022, 1:56:21 PMMay 10
to networkx-discuss
I tried using the shell layout and it produced the same output as the circular layout that I was using before.  Is there a way to use either of those layouts to specify a node to be physically in the middle like a hub?  Thanks again!
I attached a google image to show what I am trying to make it look like.
sphx_glr_plot_ego_graph_001.png
Reply all
Reply to author
Forward
0 new messages