Pydot

4.0.1 · active · verified Mon Apr 06

Pydot is a pure-Python interface to Graphviz and its DOT language, enabling users to programmatically create, read, edit, and visualize graphs. It is currently at version 4.0.1 and is actively maintained with a regular release cadence, with recent updates focusing on parser fixes and type annotations.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a directed graph using pydot, add nodes with custom styles, connect them with edges, and save the resulting visualization as a PNG image. It includes basic error handling for common Graphviz executable issues.

import pydot
import os

# Ensure Graphviz dot executable is found. On some systems or environments,
# pydot might not find it automatically. This is for demonstration; usually,
# ensuring Graphviz is in PATH is enough.
# You might need to set this if Graphviz is installed in a non-standard location.
# For example, on Windows: os.environ["PATH"] += os.pathsep + 'C:/Program Files/Graphviz/bin'

graph = pydot.Dot('my_graph', graph_type='digraph')

node_a = pydot.Node('A', style='filled', fillcolor='red')
node_b = pydot.Node('B', style='filled', fillcolor='blue')
node_c = pydot.Node('C')

graph.add_node(node_a)
graph.add_node(node_b)
graph.add_node(node_c)

graph.add_edge(pydot.Edge(node_a, node_b, label='edge_ab'))
graph.add_edge(pydot.Edge(node_b, node_c, label='edge_bc', color='green'))
graph.add_edge(pydot.Edge(node_c, node_a, label='edge_ca', arrowhead='vee'))

# Save the graph to a PNG file
try:
    graph.write_png('output_graph.png')
    print("Graph saved to output_graph.png")
except pydot.InvocationException as e:
    print(f"Error generating graph: {e}")
    print("Make sure Graphviz is installed and its 'dot' executable is in your system PATH.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →