'NetworkX draw function places nodes 'awkwardly' when there are disconnected nodes

I'm trying to draw a graph using networkx. The dataset I'm using has 2 disconnected nodes that cause the networkx to try to center both of these disconnected networks resulting in this awkward positioning in the image. Graph

Is there any way to generate a more balanced image without removing any nodes?

Link to dataset: https://snap.stanford.edu/data/p2p-Gnutella08.html

if __name__ == '__main__':
    Graphtype = nx.Graph()
    current_dataset = 'p2p'
    graph = nx.read_edgelist(
        path_names.getDatasetPath(current_dataset),
        create_using=Graphtype,
        nodetype=int
    )

    print(graph)
    
    plt.figure(figsize=(12, 12))
    nx.draw(graph, node_size=10)
    plt.savefig(current_dataset+'.png', format="PNG")
    print(nodes)


Solution 1:[1]

Yeah, this is an issue with their Fruchterman-Reingold implementation (i.e. the spring layout) that they still haven't fixed. You have several options:

a) Only plot the largest component. In your case probably sensible, as you have 6k nodes in the largest component and 2 nodes in the second largest component. However, that would remove nodes, which you seem to dislike.

b) Compute the layout for each component individually, and then arrange components in visually pleasing ways w.r.t. each other. I have an answer here to that effect.

c) Use a library that does b) for you.

enter image description here

import matplotlib.pyplot as plt
from networkx import read_edgelist
from netgraph import Graph # pip install netgraph

g = read_edgelist("p2p-Gnutella08.txt", nodetype=int)
fig, ax = plt.subplots()
Graph(g, node_size=0.2, edge_width=0.1, ax=ax)
plt.show()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul Brodersen