{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Conversion\n\nAn example showing how to use the interface to the \n`pygraphviz.AGraph` class to convert to and from graphviz.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import networkx as nx\n\nG = nx.complete_graph(5)\nA = nx.nx_agraph.to_agraph(G) # convert to a graphviz graph\nX1 = nx.nx_agraph.from_agraph(A) # convert back to networkx (but as Graph)\nX2 = nx.Graph(A) # fancy way to do conversion\nG1 = nx.Graph(X1) # now make it a Graph\n\nA.write(\"k5.dot\") # write to dot file\nX3 = nx.nx_agraph.read_dot(\"k5.dot\") # read from dotfile\n\n# You can also create .png directly with the AGraph.draw method\nA.draw(\"k5.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 10:56:16 CEST 2026.