Search code examples
pythonnetworkxhvplot

How to plot subgraph of a networkx multipartite graph as a new graph with refreshed positions


NetworkX offers G.subgraph(node_list) but when I plot it, it retains the node positions in the original full multipartite graph which is messy. I want the subgraph to be recalculated as a new plot (relatively centered) still retaining its multipartite layers but cleaner in terms of nodes vertical position. I'm using hvplot for displaying the graph using hvplot.networkx as hvnx. Additionally with this subplot from hvplot, there are some strange lines coming from the graph enter image description herewhich don't show when i just use nx.draw(G.subgraph(node_list), pos)enter image description here.

Thanks.


Solution

  • Solved both the problems.

    1. by creating new position layout by passing in the subgraph instead of main graph.
    2. corrected hvnx output by converting the graph to undirected.
    GU = G.subgraph(node_list).to_undirected()
    pos = nx.multipartite_layout(GU, subset_key="layer")
    hvnx.draw(GU, pos)