{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Four Grids\n\nDraw a 4x4 graph with matplotlib.\n\nThis example illustrates the use of keyword arguments to `networkx.draw` to\ncustomize the visualization of a simple Graph comprising a 4x4 grid.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\nimport networkx as nx\n\nG = nx.grid_2d_graph(4, 4) # 4x4 grid\n\npos = nx.spring_layout(G, iterations=100, seed=39775)\n\n# Create a 2x2 subplot\nfig, all_axes = plt.subplots(2, 2)\nax = all_axes.flat\n\nnx.draw(G, pos, ax=ax[0], font_size=8)\nnx.draw(G, pos, ax=ax[1], node_size=0, with_labels=False)\nnx.draw(\n G,\n pos,\n ax=ax[2],\n node_color=\"tab:green\",\n edgecolors=\"tab:gray\", # Node surface color\n edge_color=\"tab:gray\", # Color of graph edges\n node_size=250,\n with_labels=False,\n width=6,\n)\nH = G.to_directed()\nnx.draw(\n H,\n pos,\n ax=ax[3],\n node_color=\"tab:orange\",\n node_size=20,\n with_labels=False,\n arrowsize=10,\n width=2,\n)\n\n# Set margins for the axes so that nodes aren't clipped\nfor a in ax:\n a.margins(0.10)\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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Generated by dwww version 1.16 on Wed Apr 8 10:57:27 CEST 2026.