CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pydot

Python interface to Graphviz's Dot language for creating, reading, editing, and visualizing graphs

Pending
Overview
Eval results
Files

graph-utilities.mddocs/

Graph Building Utilities

Utility functions for creating graphs from various data structures including edge lists, adjacency matrices, and incidence matrices. These functions provide convenient ways to convert common data representations into pydot graphs.

Capabilities

Graph from Edge List

Create graphs from lists of node pairs representing edges.

def graph_from_edges(edge_list, node_prefix='', directed=False):
    """
    Creates graph from edge list.
    
    Parameters:
    - edge_list (Sequence): List of tuples representing edges [(src, dst), ...]
    - node_prefix (str): Prefix to add to node names (default: '')
    - directed (bool): Create directed graph if True, undirected if False
    
    Returns:
    Dot: Graph object with nodes and edges from the edge list
    """

Graph from Adjacency Matrix

Create graphs from square matrices where entry (i,j) indicates edge presence/weight.

def graph_from_adjacency_matrix(matrix, node_prefix='', directed=False):
    """
    Creates graph from adjacency matrix.
    
    Parameters:
    - matrix (Sequence[Sequence]): Square matrix where matrix[i][j] represents
      connection from node i to node j. Non-zero values create edges.
    - node_prefix (str): Prefix to add to node names (default: '')  
    - directed (bool): Create directed graph if True, undirected if False
    
    Returns:
    Dot: Graph object representing the adjacency relationships
    """

Graph from Incidence Matrix

Create graphs from matrices where rows represent nodes and columns represent edges.

def graph_from_incidence_matrix(matrix, node_prefix='', directed=False):
    """
    Creates graph from incidence matrix.
    
    Parameters:
    - matrix (Sequence[Sequence]): Matrix where rows are nodes and columns are edges.
      Non-zero entries indicate node participation in edges.
    - node_prefix (str): Prefix to add to node names (default: '')
    - directed (bool): Create directed graph if True, undirected if False
    
    Returns:
    Dot: Graph object representing the incidence relationships
    """

Utility Support Functions

Additional utility functions used internally and available for advanced use cases.

def call_graphviz(program, arguments, working_dir, **kwargs):
    """
    Call GraphViz programs directly.
    
    Parameters:
    - program (str): GraphViz program name ('dot', 'neato', etc.)
    - arguments (list[str]): Command line arguments
    - working_dir (str|bytes): Working directory for execution
    - **kwargs: Additional subprocess parameters
    
    Returns:
    tuple[str, str, subprocess.Popen]: (stdout, stderr, process)
    """

def quote_id_if_necessary(s, unquoted_keywords=None):
    """
    Enclose identifier in quotes if needed for DOT syntax.
    
    Parameters:
    - s (str): String to potentially quote
    - unquoted_keywords (Sequence[str], optional): Keywords that don't need quoting
    
    Returns:
    str: Quoted string if necessary, original string otherwise
    """

def quote_attr_if_necessary(s):
    """
    Enclose attribute value in quotes if needed.
    
    Parameters:
    - s (str): Attribute value to potentially quote
    
    Returns:
    str: Quoted string if necessary
    """

Usage Examples

Creating Graph from Edge List

import pydot

# Simple edge list
edges = [
    ('A', 'B'),
    ('B', 'C'), 
    ('C', 'D'),
    ('D', 'A')
]

# Create undirected graph
graph = pydot.graph_from_edges(edges, directed=False)
graph.write_png("cycle_graph.png")

# Create directed graph with node prefix
directed_graph = pydot.graph_from_edges(edges, node_prefix="node_", directed=True)
print(directed_graph.to_string())

Creating Graph from Adjacency Matrix

import pydot

# Define adjacency matrix (4x4 nodes)
# 0 = no connection, 1 = connection exists
adjacency = [
    [0, 1, 0, 1],  # Node 0 connects to nodes 1 and 3
    [1, 0, 1, 0],  # Node 1 connects to nodes 0 and 2  
    [0, 1, 0, 1],  # Node 2 connects to nodes 1 and 3
    [1, 0, 1, 0]   # Node 3 connects to nodes 0 and 2
]

# Create graph from matrix
graph = pydot.graph_from_adjacency_matrix(adjacency, node_prefix="v")
graph.set_layout("neato")  # Good for symmetric graphs
graph.write_png("adjacency_graph.png")

# Weighted adjacency matrix
weighted_adjacency = [
    [0, 5, 0, 3],
    [5, 0, 2, 0],
    [0, 2, 0, 4],
    [3, 0, 4, 0]
]

weighted_graph = pydot.graph_from_adjacency_matrix(weighted_adjacency)
# Edge weights can be accessed through the edges
for edge in weighted_graph.get_edges():
    # Weight information is embedded in the graph structure
    pass

Creating Graph from Incidence Matrix

import pydot

# Incidence matrix: rows=nodes, columns=edges
# Entry (i,j) = 1 if node i is incident to edge j
incidence = [
    [1, 1, 0, 0],  # Node 0 incident to edges 0,1
    [1, 0, 1, 0],  # Node 1 incident to edges 0,2
    [0, 1, 1, 1],  # Node 2 incident to edges 1,2,3
    [0, 0, 0, 1]   # Node 3 incident to edge 3
]

graph = pydot.graph_from_incidence_matrix(incidence, node_prefix="n")
graph.write_svg("incidence_graph.svg")

Working with Network Data

import pydot

# Example: Social network connections
social_network = [
    ('Alice', 'Bob'),
    ('Alice', 'Charlie'),
    ('Bob', 'Diana'),
    ('Charlie', 'Diana'),
    ('Diana', 'Eve'),
    ('Charlie', 'Eve')
]

# Create social network graph
social_graph = pydot.graph_from_edges(social_network, directed=False)

# Style the graph
social_graph.set_bgcolor("lightblue")
social_graph.set_layout("fdp")  # Force-directed layout for social networks

# Style nodes (people)
for node in social_graph.get_nodes():
    node.set_shape("circle")
    node.set_style("filled")
    node.set_fillcolor("lightgreen")

# Style edges (relationships)
for edge in social_graph.get_edges():
    edge.set_color("darkblue")
    edge.set_penwidth("2")

social_graph.write_png("social_network.png")

Converting from NetworkX

# If you have NetworkX graphs, you can convert to edge list first
import networkx as nx
import pydot

# Create NetworkX graph
nx_graph = nx.erdos_renyi_graph(10, 0.3)

# Convert to edge list
edge_list = list(nx_graph.edges())

# Create pydot graph
pydot_graph = pydot.graph_from_edges(edge_list, node_prefix="node_")
pydot_graph.write_png("random_graph.png")

Install with Tessl CLI

npx tessl i tessl/pypi-pydot

docs

file-operations.md

graph-management.md

graph-utilities.md

index.md

subgraphs-clusters.md

tile.json