{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Attributes\n\nExample illustrating how attributes of nodes, edges, and graphs are handled\nduring conversion to/from `~pygraphviz.AGraph`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import networkx as nx\n\nG = nx.Graph()\nG.add_edge(1, 2, color=\"red\")\nG.add_edge(2, 3, color=\"red\")\nG.add_node(3)\nG.add_node(4)\n\nA = nx.nx_agraph.to_agraph(G) # convert to a graphviz graph\nA.draw(\"attributes.png\", prog=\"neato\") # Draw with pygraphviz\n\n# convert back to networkx Graph with attributes on edges and\n# default attributes as dictionary data\nX = nx.nx_agraph.from_agraph(A)\nprint(\"edges\")\nprint(list(X.edges(data=True)))\nprint(\"default graph attributes\")\nprint(X.graph)\nprint(\"node node attributes\")\nprint(X.nodes.data(True))"
]
}
],
"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:21 CEST 2026.