Search code examples
pythonnetworkxbipartitedigraphs

Bipartite graph in NetworkX for LARGE amount of nodes


I am trying to create bipartite of certain nodes, for small numbers it looks perfectly fine:

Image for around 30 nodes

Unfortunately, this isn't the case for more nodes like this one:

Image for more nodes

My code for determining the position of each node looks something like this:

pos = {}
pos[SOURCE_STRING] = (0, width/2)
row = 0
for arr in left_side.keys():
    pos[str(arr).replace(" ","")]=(NODE_SIZE, row)
    row += NODE_SIZE
row = 0
for arr in right_side.keys():
    pos[str(arr).replace(" ","")]=(2*NODE_SIZE,row)
    row += NODE_SIZE
pos[SINK_STRING] = (3*NODE_SIZE, width/2)
return pos

And then I feed it to the DiGraph class:

G = nx.DiGraph()
G.add_nodes_from(nodes)
G.add_edges_from(edges, len=1)
nx.draw(G, pos=pos ,node_shape = "s", with_labels = True,node_size=NODE_SIZE)

This doesn't make much sense since they should be in the same distance from each other since NODE_SIZE is constant it doesn't change for the rest of the program.

Following this thread:

Bipartite graph in NetworkX

Didn't help me either.

Can something be done about this?

Edit(Following Paul Brodersen Advice using netGraph:

Used this documentation: netgraph doc

And still got somewhat same results, such as: netgraph try

Using edges and different positions, also played with node size, with no success.

Code:

netgraph.Graph(edges, node_layout='bipartite', node_labels=True)
plt.show()

Solution

  • In your netgraph call, you are not changing the node size. My suggestion with 30 nodes:

    enter image description here

    import numpy as np
    import matplotlib.pyplot as plt
    
    from netgraph import Graph
    
    edges = np.vstack([np.random.randint(0, 15, 60),
                       np.random.randint(16, 30, 60)]).T
    
    Graph(edges, node_layout='bipartite', node_size=0.5, node_labels=True, node_label_offset=0.1, edge_width=0.1)
    plt.show()
    

    With 100 nodes:

    enter image description here

    import numpy as np
    import matplotlib.pyplot as plt
    
    from netgraph import Graph
    
    edges = np.vstack([np.random.randint(0, 50, 200),
                       np.random.randint(51, 100, 200)]).T
    
    Graph(edges, node_layout='bipartite', node_size=0.5, node_labels=True, node_label_offset=0.1, edge_width=0.1)
    plt.show()