{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# 2D Grid\n\nIllustrate `~networkx.drawing.nx_agraph.write_dot` in conjunction with the\nGraphviz command line interface to create visualizations.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\nimport networkx as nx"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a dot file from a networkx graph for further processing with graphviz.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"G = nx.grid_2d_graph(5, 5) # 5x5 grid\n# This example needs Graphviz and PyGraphviz\nnx.nx_agraph.write_dot(G, \"grid.dot\")\n# Having created the dot file, graphviz can be invoked via the command line\n# to generate an image on disk, e.g.\nprint(\"Now run: neato -Tps grid.dot >grid.ps\")\n\n# Alternatively, the and image can be created directly via AGraph.draw\nA = nx.nx_agraph.to_agraph(G)\nA.draw(\"5x5.png\", prog=\"neato\")"
]
}
],
"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:16:32 CEST 2026.