I generate graphs from a big set of JSON files that I don't have a priory info about node positions in a graph image. As a result, when I draw these graphs, I get images with nodes and edges unevenly arranged in the image with lots of unused empty space.
The following is an example of a program that generates a connected graph of 38 nodes. With default NetworkX image size connected nodes overlap each other. And with increased image size the large empty spaces appear.
How to create layout that will arrange nodes and edges evenly taking into account image size without large empty spaces?
import networkx as nx
import matplotlib.pyplot as plt
import random
import string
def generate_label(i):
label = str(i)+':'+random.choice(['q','a'])+':' \
+''.join(random.sample(string.ascii_letters, 3))
return label
edges = [[0, 16], [1, 13], [2, 20], [17, 2], [3, 28], [17, 3], [4, 27],
[17, 4], [7, 26], [17, 7], [21, 9], [29, 10], [31, 11], [32, 12],
[1, 13], [0, 16], [17, 18], [17, 2], [17, 21], [17, 22], [17, 3],
[17, 4], [17, 29], [17, 24], [17, 7], [18, 19], [17, 18], [18, 19],
[2, 20], [21, 9], [17, 21], [22, 23], [17, 22], [22, 23], [24, 25],
[17, 24], [24, 25], [7, 26], [4, 27], [3, 28], [29, 10], [17, 29],
[30, 31], [30, 32], [30, 33], [30, 31], [31, 11], [30, 32], [32, 12],
[30, 33], [34, 35], [34, 35]]
G = nx.Graph()
for i in range(38):
G.add_node(i, label = generate_label(i))
for e in edges:
G.add_edge(e[0], e[1])
labels = nx.get_node_attributes(G, 'label')
plt.figure(figsize=(14,20))
nx.draw_networkx(nx.relabel_nodes(G, labels), with_labels=True,
node_color = 'orange', node_size=200, font_size=12)
plt.show()