S. L.
S. L.

Reputation: 690

Python networkx node spacing

I have 1000 different names, each constituting a node. Each name can be connected with 0..1000 other names an unlimited amount of times. I would like to graph it in such a way that the distance between two nodes is inversely proportional to the number of times they are connected.

Example:

'node1' : ['node2','node2','node2','node2','node2','node3']
'node2' : ['node1','node1','node1','node1','node1']
'node3' : ['node1']

node1and node2 should huddle together and node3 should be further away.

Is that possible? Currently I'm graphing using the following code:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_nodes_from(grapharr.keys())
for k in grapharr:
    for j in grapharr[k]:
        G.add_edge(k,j)

nx.draw_networkx(G, **options)

grapharris a dict structure where the keys are nodes and the values are arrays containing the connections for the particular node.

Upvotes: 2

Views: 3488

Answers (1)

vurmux
vurmux

Reputation: 10030

It is impossible in the general case. Look at this graph:

enter image description here

Imagine that the central node has a thousand connections to each other, but 'square' nodes have only one connection between them. How will you draw them?

Anyway, you can set the connectivity level as edge weight and use force-directed layouts that will try to create the best layout (but not 100% optimal, of course). In networkx, there are:

Upvotes: 1

Related Questions