Python interface to Graphviz's Dot language for creating, reading, editing, and visualizing graphs
—
Functions and methods for reading DOT files and strings, plus comprehensive output generation in various formats including images, vector graphics, and DOT files.
Functions for parsing DOT language input from files and strings to create pydot graph objects.
def graph_from_dot_file(path, encoding=None):
"""
Load graphs from DOT file.
Parameters:
- path (str|bytes): Path to DOT file
- encoding (str, optional): File encoding (default: None for auto-detection)
Returns:
list[Dot]|None: List of Dot objects or None if parsing fails
"""
def graph_from_dot_data(s):
"""
Create graphs from DOT description string.
Parameters:
- s (str): DOT language string
Returns:
list[Dot]|None: List of Dot objects or None if parsing fails
"""The Dot class provides comprehensive output generation through dynamically created methods for each supported format.
Generate raster and vector image files:
# PNG format
def create_png(self, prog=None, encoding=None):
"""Create PNG image as bytes."""
def write_png(self, path, prog=None, encoding=None):
"""Write PNG image to file."""
# SVG format
def create_svg(self, prog=None, encoding=None):
"""Create SVG vector image as bytes."""
def write_svg(self, path, prog=None, encoding=None):
"""Write SVG vector image to file."""
# PDF format
def create_pdf(self, prog=None, encoding=None):
"""Create PDF document as bytes."""
def write_pdf(self, path, prog=None, encoding=None):
"""Write PDF document to file."""
# JPEG format
def create_jpeg(self, prog=None, encoding=None):
"""Create JPEG image as bytes."""
def write_jpeg(self, path, prog=None, encoding=None):
"""Write JPEG image to file."""
# GIF format
def create_gif(self, prog=None, encoding=None):
"""Create GIF image as bytes."""
def write_gif(self, path, prog=None, encoding=None):
"""Write GIF image to file."""Generate DOT language representations:
def create_dot(self, prog=None, encoding=None):
"""
Create GraphViz-processed DOT output as bytes.
This version is processed by GraphViz and includes layout information.
"""
def write_dot(self, path, prog=None, encoding=None):
"""Write GraphViz-processed DOT output to file."""
def write_raw(self, path):
"""
Write raw pydot DOT output to file.
This version is generated directly by pydot without GraphViz processing.
"""
def to_string(self):
"""
Generate raw DOT string representation.
Returns:
str: DOT language representation
"""Support for specialized formats:
# PostScript formats
def create_ps(self, prog=None, encoding=None): ...
def write_ps(self, path, prog=None, encoding=None): ...
def create_ps2(self, prog=None, encoding=None): ...
def write_ps2(self, path, prog=None, encoding=None): ...
# Plain text formats
def create_plain(self, prog=None, encoding=None): ...
def write_plain(self, path, prog=None, encoding=None): ...
# Interactive formats
def create_xdot(self, prog=None, encoding=None): ...
def write_xdot(self, path, prog=None, encoding=None): ...
# Map formats for web
def create_cmap(self, prog=None, encoding=None): ...
def write_cmap(self, path, prog=None, encoding=None): ...
def create_cmapx(self, prog=None, encoding=None): ...
def write_cmapx(self, path, prog=None, encoding=None): ...Complete list of supported formats (23 total): canon, cmap, cmapx, cmapx_np, dia, dot, fig, gd, gd2, gif, hpgl, imap, imap_np, ismap, jpe, jpeg, jpg, mif, mp, pcl, pdf, pic, plain, plain-ext, png, ps, ps2, svg, svgz, vml, vmlz, vrml, vtx, wbmp, xdot, xlib
All output methods accept optional parameters:
# Parameters for all create_* and write_* methods:
# - prog (str, optional): GraphViz program to use ('dot', 'neato', 'fdp', 'sfdp', 'twopi', 'circo')
# - encoding (str, optional): Output encoding (default: None)import pydot
# Load graph from file
graphs = pydot.graph_from_dot_file("network.dot")
if graphs:
graph = graphs[0] # Get first graph
print(f"Loaded graph: {graph.get_name()}")
# Load with specific encoding
graphs = pydot.graph_from_dot_file("unicode_graph.dot", encoding="utf-8")dot_string = '''
digraph G {
A -> B [label="edge"];
B -> C [color=blue];
C -> A [style=dashed];
}
'''
graphs = pydot.graph_from_dot_data(dot_string)
if graphs:
graph = graphs[0]
print(f"Nodes: {len(graph.get_nodes())}")
print(f"Edges: {len(graph.get_edges())}")import pydot
# Create a simple graph
graph = pydot.Dot("output_example", graph_type="digraph")
graph.add_node(pydot.Node("A", shape="box"))
graph.add_node(pydot.Node("B", shape="circle"))
graph.add_edge(pydot.Edge("A", "B"))
# Generate PNG image
graph.write_png("diagram.png")
# Generate SVG for web
graph.write_svg("diagram.svg")
# Generate PDF for printing
graph.write_pdf("diagram.pdf")
# Get raw DOT string
dot_source = graph.to_string()
print(dot_source)
# Get GraphViz-processed DOT with layout
processed_dot = graph.create_dot()
with open("processed.dot", "wb") as f:
f.write(processed_dot)# Use different GraphViz programs for layout
graph.write_png("dot_layout.png", prog="dot") # Hierarchical
graph.write_png("neato_layout.png", prog="neato") # Spring model
graph.write_png("circo_layout.png", prog="circo") # Circular
graph.write_png("fdp_layout.png", prog="fdp") # Force-directed# Get image data as bytes for further processing
png_data = graph.create_png()
svg_data = graph.create_svg()
# Use with web frameworks, image processing libraries, etc.
from PIL import Image
import io
image = Image.open(io.BytesIO(png_data))
image.show()Install with Tessl CLI
npx tessl i tessl/pypi-pydot