Note
Click here to download the full example code
Multi-partite & shell node layouts
Draw a multi-partite in successive layers or in concentric circles.
To draw a multi-partite graph in successive layers, use the multipartite
node layout.
The layers
argument indicates in which layer each node is plotted, as well as the order of layers.
import numpy as np
import matplotlib.pyplot as plt
from netgraph import Graph
partitions = [
list(range(3)),
list(range(3, 9)),
list(range(9, 21))
]
edges = list(zip(np.repeat(partitions[0], 2), partitions[1])) \
+ list(zip(np.repeat(partitions[0], 2), partitions[1][1:])) \
+ list(zip(np.repeat(partitions[1], 2), partitions[2])) \
+ list(zip(np.repeat(partitions[1], 2), partitions[2][1:]))
Graph(edges, node_layout='multipartite', node_layout_kwargs=dict(layers=partitions, reduce_edge_crossings=True), node_labels=True)
plt.show()
Out:
/home/paul/src/netgraph/netgraph/_parser.py:23: UserWarning: Multi-graphs are not properly supported. Duplicate edges are plotted as a single edge; edge weights (if any) are summed.
warnings.warn(msg)
<string>:6: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
To change the layout from the left-right orientation to a bottom-up orientation, call the layout function directly and swap x and y coordinates of the node positions.
import numpy as np
import matplotlib.pyplot as plt
from netgraph import Graph, get_multipartite_layout
partitions = [
list(range(3)),
list(range(3, 9)),
list(range(9, 21))
]
edges = list(zip(np.repeat(partitions[0], 2), partitions[1])) \
+ list(zip(np.repeat(partitions[0], 2), partitions[1][1:])) \
+ list(zip(np.repeat(partitions[1], 2), partitions[2])) \
+ list(zip(np.repeat(partitions[1], 2), partitions[2][1:]))
node_positions = get_multipartite_layout(edges, partitions, reduce_edge_crossings=True)
node_positions = {node : (x, y) for node, (y, x) in node_positions.items()}
Graph(edges, node_layout=node_positions, node_labels=True)
plt.show()
Out:
<string>:6: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
/home/paul/src/netgraph/netgraph/_parser.py:23: UserWarning: Multi-graphs are not properly supported. Duplicate edges are plotted as a single edge; edge weights (if any) are summed.
warnings.warn(msg)
To draw a multi-partite graph in concentric circles, use the shell
node layout.
The shells
argument indicates in which circle each node is plotted, as well as the order of shells.
import numpy as np
import matplotlib.pyplot as plt
from netgraph import Graph
partitions = [
list(range(3)),
list(range(3, 9)),
list(range(9, 21))
]
edges = list(zip(np.repeat(partitions[0], 2), partitions[1])) \
+ list(zip(np.repeat(partitions[0], 2), partitions[1][1:])) \
+ list(zip(np.repeat(partitions[1], 2), partitions[2])) \
+ list(zip(np.repeat(partitions[1], 2), partitions[2][1:]))
Graph(edges, node_layout='shell', node_layout_kwargs=dict(shells=partitions, reduce_edge_crossings=True), node_labels=True)
plt.show()
Out:
/home/paul/src/netgraph/netgraph/_parser.py:23: UserWarning: Multi-graphs are not properly supported. Duplicate edges are plotted as a single edge; edge weights (if any) are summed.
warnings.warn(msg)
<string>:6: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
Total running time of the script: ( 0 minutes 0.550 seconds)