0

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()

enter image description here

dokondr
  • 3,389
  • 12
  • 38
  • 62
  • [This previous answer](https://stackoverflow.com/a/53156709/2912349) of mine may be helpful. – Paul Brodersen Jan 24 '23 at 12:02
  • Many thanks, looks great! Is it possible to draw two graphs side by side with netgraph? Any example of that? – dokondr Jan 24 '23 at 21:08
  • Sure, it is just matplotlib under the hood: `fig, (ax1, ax2) = plt.subplots(1, 2); g1 = Graph(..., ax=ax1); g2 = Graph(..., ax=ax2); plt.show()`. The full documentation can be perused [here](https://netgraph.readthedocs.io/en/latest/index.html). – Paul Brodersen Jan 25 '23 at 10:28
  • I have tried netgraph and got this error: `...\netgraph\_node_layout.py:918: UserWarning: Maximum number of iterations reached. Aborting further node layout optimisations. warnings.warn("Maximum number of iterations reached. Aborting further node layout optimisations.")` – dokondr Jan 25 '23 at 10:49
  • This just seems to be a warning? If you run into issues, please either raise an issue on my github, including a reproducible example and the full error trace, or ask a new question here on SO with the `netgraph` tag. The comment section of a previous question is not the ideal place for such a discussion. – Paul Brodersen Jan 25 '23 at 11:12

0 Answers0