Skip to content

faranalytics/pydot_graph

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pydot Graph

A graph notation for creating Graphviz visualizations using Pydot.

Introduction

Pydot Graph makes it a little easier to assemble graphs using Pydot. You can, for example, use Pydot Graph to construct a graph programmatically at each step of a pipeline - visually documenting a data transformation.

Example

In this example a hypothetical graph is constructed using a Pydot Graph representation. Pydot Graph represents the graph as a list of Pydot nodes. A list of lists is used in order to represent branches in the graph.

In order to understand the Pydot Graph representation, you can visually map the labels in the graph image to the labels in the Python list of Pydot nodes and edges.

Instructions

  1. Import the dependencies.
  2. Construct the graph using a list of lists.
    a. Construct a set of nodes that will have multiple references.
    b. Construct the main graph.
  3. Create an instance of PydotGraph.
  4. Build the graph.
  5. Write the graph to a file.
  6. Display the image.

Implementation

# 0. Import the dependencies.
from pydot_graph import PydotGraph
import random
import pydot
from IPython.display import Image, display


def random_color():
    return "#" + hex(random.randint(0, 0xFFFFFF))[2:].rjust(6, "0")

# 1. Construct the graph using a list of lists.

## 1a. Contruct a set of nodes that will have multiple references.
nodes = [
    pydot.Node(label=f"Node Label 5a.\nn={100}", fillcolor=random_color()),
    pydot.Edge(label="Edge 9."),
    pydot.Node(label=f"Node Label 6a.\nn={100}", fillcolor=random_color()),
]

## 1b. Construct the main graph.
pydot_graph = [
    pydot.Dot(graph_type="digraph", rankdir="TB"),
    pydot.Node(label=f"Node Label 1.\nn={100}", fillcolor=random_color()),
    pydot.Edge(label="Edge 2"),
    pydot.Node(label=f"Node Label 2.\nn={100}", fillcolor=random_color()),
    pydot.Edge(dir="none", label="A"),
    pydot.Node(shape="point", color="black", height=0, width=0),
    pydot.Edge(label="Edge 3a."),
    [
        pydot.Cluster(style="filled", color="lightgrey"),
        pydot.Node(label=f"Node Label 3a.\nn={100}", fillcolor=random_color()),
        pydot.Edge(label="Edge 3b."),
        pydot.Node(label=f"Node Label 3b.\nn={100}", fillcolor=random_color()),
        pydot.Edge(label="Edge 12a."),
        [
            pydot.Cluster(style="filled", color="yellow"),
            pydot.Node(label=f"Node Label 30a.\nn={100}", fillcolor=random_color()),
            pydot.Edge(label="Edge 30b."),
            pydot.Node(label=f"Node Label 30b.\nn={100}", fillcolor=random_color()),
        ],
        pydot.Edge(label="Edge 11a."),
        [
            pydot.Cluster(style="filled", color="blue"),
            pydot.Node(label=f"Node Label 10a.\nn={100}", fillcolor=random_color()),
            pydot.Edge(label="Edge 10b."),
            pydot.Node(label=f"Node Label 10b.\nn={100}", fillcolor=random_color()),
        ],
        pydot.Edge(label="Edge 6."),
        nodes,
    ],
    pydot.Edge(label="Edge 4."),
    pydot.Node(label=f"Node Label 4.\nn={100}", fillcolor=random_color()),
    pydot.Edge(label="Edge 7."),
    nodes,
]

# 2. Create an instance of PydotGraph.
pydotGraph = PydotGraph(
    node_defaults={"shape": "box", "fontname": "Sans", "style": "filled", "fillcolor": "#eeeeee"},
    edge_defaults={"fontname": "Sans"},
)

# 3. Build the graph.
graph = pydotGraph.build(pydot_graph)

# 4. Write the graph to a file.
graph.write_png("output.png")

# 5. Display the image.
Image(graph.create_png())

Output

About

A graph notation for creating Graphviz visualizations using Pydot.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages