1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# DAG - Topological Layout\n\nThis example combines the `topological_generations` generator with\n`multipartite_layout` to show how to visualize a DAG in topologically-sorted\norder.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import networkx as nx\nimport matplotlib.pyplot as plt\n\n\nG = nx.DiGraph(\n [\n (\"f\", \"a\"),\n (\"a\", \"b\"),\n (\"a\", \"e\"),\n (\"b\", \"c\"),\n (\"b\", \"d\"),\n (\"d\", \"e\"),\n (\"f\", \"c\"),\n (\"f\", \"g\"),\n (\"h\", \"f\"),\n ]\n)\n\nfor layer, nodes in enumerate(nx.topological_generations(G)):\n # `multipartite_layout` expects the layer as a node attribute, so add the\n # numeric layer value as a node attribute\n for node in nodes:\n G.nodes[node][\"layer\"] = layer\n\n# Compute the multipartite_layout using the \"layer\" node attribute\npos = nx.multipartite_layout(G, subset_key=\"layer\")\n\nfig, ax = plt.subplots()\nnx.draw_networkx(G, pos=pos, ax=ax)\nax.set_title(\"DAG layout in topological order\")\nfig.tight_layout()\nplt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.15"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|