{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Simple graph\n\nDraw simple graph with manual layout.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import networkx as nx\nimport matplotlib.pyplot as plt\n\nG = nx.Graph()\nG.add_edge(1, 2)\nG.add_edge(1, 3)\nG.add_edge(1, 5)\nG.add_edge(2, 3)\nG.add_edge(3, 4)\nG.add_edge(4, 5)\n\n# explicitly set positions\npos = {1: (0, 0), 2: (-1, 0.3), 3: (2, 0.17), 4: (4, 0.255), 5: (5, 0.03)}\n\noptions = {\n \"font_size\": 36,\n \"node_size\": 3000,\n \"node_color\": \"white\",\n \"edgecolors\": \"black\",\n \"linewidths\": 5,\n \"width\": 5,\n}\nnx.draw_networkx(G, pos, **options)\n\n# Set margins for the axes so that nodes aren't clipped\nax = plt.gca()\nax.margins(0.20)\nplt.axis(\"off\")\nplt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A directed graph\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"G = nx.DiGraph([(0, 3), (1, 3), (2, 4), (3, 5), (3, 6), (4, 6), (5, 6)])\n\n# group nodes by column\nleft_nodes = [0, 1, 2]\nmiddle_nodes = [3, 4]\nright_nodes = [5, 6]\n\n# set the position according to column (x-coord)\npos = {n: (0, i) for i, n in enumerate(left_nodes)}\npos.update({n: (1, i + 0.5) for i, n in enumerate(middle_nodes)})\npos.update({n: (2, i + 0.5) for i, n in enumerate(right_nodes)})\n\nnx.draw_networkx(G, pos, **options)\n\n# Set margins for the axes so that nodes aren't clipped\nax = plt.gca()\nax.margins(0.20)\nplt.axis(\"off\")\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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Generated by dwww version 1.16 on Wed Apr 8 11:18:07 CEST 2026.