'Displaying multiple graphs from networkx in a table

I've been playing around with the random graph feature of networkx as seen here with the Erdos-Renyi graph:

G = nx.gnp_random_graph(n, p, seed=None, directed=False)

I can then draw the graph with

nx.draw 

Is there a way, I can make a table of random graph images using nx.draw? I want to make a table of some sampled graphs with some labels. Is there a way to do this using Matlab plot?



Solution 1:[1]

If I understand correclty, you can use subplots to achieve what you want:

fig, axes = plt.subplots(nrows=3, ncols=3)

for ax in axes.ravel():
    G = nx.gnp_random_graph(10,10, seed=None, directed=False)
    nx.draw_networkx(G, ax=ax)

Edit:

You can change the size of the figure at instantiation, by using:

fig, axes = plt.subplots(nrows=rows, ncols=cols, figsize=(10,10)) # default unit is inches. 

You can change the size after the fact by doing:

fig.set_figwidth(10)
and 
fig.set_figheight(10)

you can access individual subplots if you have more than 1 row and more than 1 column, like so:

axes[row,column] # zero-indexed. 

to add labels or other stuff, you can do:

axes[row,column].set_ylabel('blah')
axes[row,column].set_title('blubb')

to change the figure title you can do:

fig.suptitle('my fancy title')

If at the end your labels intersect or your figure looks otherwise messy, you can enforce tight layout:

plt.tight_layout()

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