简体   繁体   中英

NetworkX: draw graph in layers

I have a graph which is divided on levels iefe :

ids : 0 - 100 are lowest level
ids : 101 - 500 are level 2
ids : 501 - 1500 are level 3
and so on ...

Is there some way to force the graph to draw the nodes in the levels organized in layers, one above the other.

I want to stack them without overflow :)

In my case in which layer the node is depends on the node-id, but it could be some other organizational principle, if you have some idea.


This so far seems like possible solution :

def plot(self):
    plt.figure()
    pos = nx.graphviz_layout(self.g,prog='dot')
    nx.draw(self.g, pos, node_size=650, node_color='#ffaaaa')

Five layers example ...

在此处输入图片说明

The layout functions, such as nx.spring_layout , return a dict whose keys are nodes and whose values are 2-tuples (coordinates). Here is an example of what the pos dict might look like:

In [101]: pos
Out[101]: 
{(0, 0): array([ 0.70821816,  0.03766149]),
 (0, 1): array([ 0.97041253,  0.30382541]),
 (0, 2): array([ 0.99647583,  0.63049339]),
 (0, 3): array([ 0.86691957,  0.86393669]),
 (1, 0): array([ 0.79471631,  0.08748146]),
 (1, 1): array([ 0.71731384,  0.35520076]),
 (1, 2): array([ 0.69295087,  0.71089292]),
 (1, 3): array([ 0.63927851,  1.        ]),
 (2, 0): array([ 0.42228877,  0.        ]),
 (2, 1): array([ 0.33250362,  0.3165331 ]),
 (2, 2): array([ 0.31084694,  0.69246818]),
 (2, 3): array([ 0.34141212,  0.9952164 ]),
 (3, 0): array([ 0.16734454,  0.11357547]),
 (3, 1): array([ 0.01560951,  0.33063389]),
 (3, 2): array([ 0.        ,  0.63044189]),
 (3, 3): array([ 0.12242227,  0.85656669])}

You can then manipulate these coordinates further, any way you please. For example, since the x and y coordinates returned by spring_layout are between 0 and 1, you could add 10 times the layer level value to the y -coordinate to separate the nodes into layers:

for node in pos:
    level = node // nodes_per_layer
    pos[node] += (0,10*level)

import networkx as nx
import matplotlib.pyplot as plt

layers = 5
nodes_per_layer = 3
n = layers * nodes_per_layer
p = 0.2

G = nx.fast_gnp_random_graph(n, p, seed=2017, directed=True)
pos = nx.spring_layout(G, iterations=100)

for node in pos:
    level = node // nodes_per_layer
    pos[node] += (0,10*level)

nx.draw(G, pos, node_size=650, node_color='#ffaaaa', with_labels=True)
plt.show()

produces在此处输入图片说明

Have you considered https://github.com/SkBlaz/Py3Plex ? It has proper support for multilayer networks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM