Bipartite graph in NetworkX

sheetal_158 picture sheetal_158 · Nov 23, 2014 · Viewed 12k times · Source
B.add_nodes_from(a, bipartite=1)
B.add_nodes_from(b, bipartite=0)
nx.draw(B, with_labels = True)  
plt.savefig("graph.png")

I am getting the following figure. How can I make it look like a proper bipartite graph?

My graph

Answer

mdml picture mdml · Nov 23, 2014

You could do something like this, to draw nodes from each partition at a particular x coordinate:

X, Y = bipartite.sets(B)
pos = dict()
pos.update( (n, (1, i)) for i, n in enumerate(X) ) # put nodes from X at x=1
pos.update( (n, (2, i)) for i, n in enumerate(Y) ) # put nodes from Y at x=2
nx.draw(B, pos=pos)
plt.show()

bipartite-graph

The key is creating the dict for the the nx.draw pos parameter, which is:

A dictionary with nodes as keys and positions as values.

See the docs.