{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Spectral Embedding\n\nThe spectral layout positions the nodes of the graph based on the\neigenvectors of the graph Laplacian $L = D - A$, where $A$ is the\nadjacency matrix and $D$ is the degree matrix of the graph.\nBy default, the spectral layout will embed the graph in two\ndimensions (you can embed your graph in other dimensions using the\n``dim`` argument to either :func:`~drawing.nx_pylab.draw_spectral` or\n:func:`~drawing.layout.spectral_layout`).\n\nWhen the edges of the graph represent similarity between the incident\nnodes, the spectral embedding will place highly similar nodes closer\nto one another than nodes which are less similar.\n\nThis is particularly striking when you spectrally embed a grid\ngraph. In the full grid graph, the nodes in the center of the\ngraph are pulled apart more than nodes on the periphery.\nAs you remove internal nodes, this effect increases.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\nimport networkx as nx\n\n\noptions = {\"node_color\": \"C0\", \"node_size\": 100}\n\nG = nx.grid_2d_graph(6, 6)\nplt.subplot(332)\nnx.draw_spectral(G, **options)\n\nG.remove_edge((2, 2), (2, 3))\nplt.subplot(334)\nnx.draw_spectral(G, **options)\n\nG.remove_edge((3, 2), (3, 3))\nplt.subplot(335)\nnx.draw_spectral(G, **options)\n\nG.remove_edge((2, 2), (3, 2))\nplt.subplot(336)\nnx.draw_spectral(G, **options)\n\nG.remove_edge((2, 3), (3, 3))\nplt.subplot(337)\nnx.draw_spectral(G, **options)\n\nG.remove_edge((1, 2), (1, 3))\nplt.subplot(338)\nnx.draw_spectral(G, **options)\n\nG.remove_edge((4, 2), (4, 3))\nplt.subplot(339)\nnx.draw_spectral(G, **options)\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:58:33 CEST 2026.