{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Self-loops\n\nA self-loop is an edge that originates from and terminates the same node.\nThis example shows how to draw self-loops with `nx_pylab`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import networkx as nx\nimport matplotlib.pyplot as plt\n\n# Create a graph and add a self-loop to node 0\nG = nx.complete_graph(3, create_using=nx.DiGraph)\nG.add_edge(0, 0)\npos = nx.circular_layout(G)\n\n# As of version 2.6, self-loops are drawn by default with the same styling as\n# other edges\nnx.draw(G, pos, with_labels=True)\n\n# Add self-loops to the remaining nodes\nedgelist = [(1, 1), (2, 2)]\nG.add_edges_from(edgelist)\n\n# Draw the newly added self-loops with different formatting\nnx.draw_networkx_edges(G, pos, edgelist=edgelist, arrowstyle=\"<|-\", style=\"dashed\")\n\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 10:56:14 CEST 2026.