If you know how to use Matplotlib, you are probably able to answer without knowing about NetworkX.
I have 2 networks to draw using NetworkX, and I'd like to draw side by side in a single graphic, showing the axes of each. Essentially, it's a matter of creating 2 subplots in Matplotlib (which is the library that NetworkX uses to draw graphs).
The positions of each node of the networks are distributed in an area of [0, area_size], but usually there is no point with coordinates x = 0.0 or y = area_size. That is, points appear inside an area of [0, area_size] or smaller. Not bigger.
The proportions of each subgraph should be 0.8 x 1.0, separated by an area with proportions of 0.16 x 1.0.
In essence, it should look like this (assuming area_size = 100).
Then I have to draw lines between the 2 plots, so I need some way to get back to the positions in each graph, in order to connect them.
Node positions are generated, stored and assigned like this
# generate and store node positions
positions = {}
for node_id in G.nodes():
pos_x = # generate pos_x in [0.0, area_size]
pos_y = # generate pos_y in [0.0, area_size]
positions[node_id]['x'] = pos_x
positions[node_id]['y'] = pos_y
G.node[node_id]['x'] = pos_x
G.node[node_id]['y'] = pos_y
These positions are then stored in a dictionary pos = {node_id: (x, y), ...}
. NetworkX gets this structure to draw nodes in the correct positions nx.draw_network(G, pos=positions)
.
Right now, I do the following:
- calculate the positions of the first network on the [0, area_size], then stretch them to [0, area_size*0.8]
- calculate the positions of the second network in the same way
- shift the positions of the second network to the right, summing area_size*0.8 + area_size*0.16 to the x coordinates
- set the figure size (in inches)
plt.figure(figsize=(h, w), dpi=100)
- set the x axis
plt.xlim(0.0, area_size*0.8*2 + area_size*0.16)
- set the y axis
plt.ylim(0.0, area_size)
- draw the first network (passing its positions)
- draw the second network (passing its positions)
- draw lines between the two networks
The plot I get has the right proportions, but the axis does not display the right information. Should I hide the axis, and draw dummy ones?
I probably need some better procedure to draw proper subplots.
I also noticed that the figure size I set is not always respected when I export to pdf.
The function NetworkX uses to draw a graph are these, in particular, I'm using draw_networkx.
I don't really mind drawing the separate axes, if they give too much trouble.