PyGraphviz

1.14 · active · verified Wed Apr 15

PyGraphviz is a Python interface to the Graphviz graph layout and visualization package. It enables users to create, edit, read, write, and draw graphs using Python by accessing Graphviz's graph data structure and layout algorithms. It offers a programming interface similar to NetworkX. The current stable version is 1.14, with minor releases typically occurring a few times a year, alongside less frequent major version updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a directed graph, add nodes and edges with attributes, set global graph attributes, lay out the graph using a specified engine (e.g., 'dot'), and render it to a PNG image file.

import pygraphviz as pgv

# Create a directed graph
G = pgv.AGraph(directed=True, strict=False)

# Add nodes
G.add_node('A', shape='box', color='red')
G.add_node('B', shape='circle')
G.add_node('C', label='Node C')

# Add edges
G.add_edge('A', 'B', label='connects', penwidth=2)
G.add_edge('B', 'C', color='blue', style='dashed')
G.add_edge('C', 'A')

# Set graph attributes
G.graph_attr['label'] = 'My Sample Graph'
G.graph_attr['overlap'] = 'false'
G.graph_attr['splines'] = 'true'

# Layout the graph (using dot engine by default)
# and draw to a file
G.layout(prog='dot')
output_filename = 'sample_graph.png'
G.draw(output_filename)

print(f"Graph saved to {output_filename}")
# print(G) # Uncomment to see the DOT language representation

view raw JSON →