Skip to main content

Python interface to Graphviz's Dot

Project description

Image for: Project description

Pydot

pydot is a Python interface to Graphviz and its DOT language. You can use pydot to create, read, edit, and visualize graphs.

  • It's made in pure Python, with only one dependency – pyparsing – other than Graphviz itself.
  • It's compatible with networkx, which can convert its graphs to pydot.

To see what Graphviz is capable of, check the Graphviz Gallery!

Dependencies

Image for: Dependencies
  • pyparsing: used only for loading DOT files, installed automatically during pydot installation.
  • GraphViz: used to render graphs in a variety of formats, including PNG, SVG, PDF, and more. Should be installed separately, using your system's package manager, something similar (e.g., MacPorts), or from its source.

Installation

Image for: Installation
  • Latest release, from PyPI:

    pip install pydot
    
  • Current development code, from this repository:

    pip install git+https://github.com/pydot/pydot.git
    
  • Development installation, to modify the code or contribute changes:

    # Clone the repository
    git clone https://github.com/pydot/pydot
    cd pydot
    
    # (Optional: create a virtual environment)
    python3 -m venv _venv
    . ./_venv/bin/activate
    
    # Make an editable install of pydot from the source tree
    pip install -e .
    

Quickstart

Image for: Quickstart

1. Input

No matter what you want to do with pydot, you'll need some input to start with. Here are the common ways to get some data to work with.

Import a graph from an existing DOT file

Let's say you already have a file example.dot (based on an example from Wikipedia):

graph my_graph {
    bgcolor="yellow";
    a [label="Foo"];
    b [shape=circle];
    a -- b -- c [color=blue];
}

You can read the graph from the file in this way:

import pydot

graphs = pydot.graph_from_dot_file("example.dot")
graph = graphs[0]

Parse a graph from an existing DOT string

Use this method if you already have a string describing a DOT graph:

import pydot

dot_string = """graph my_graph {
    bgcolor="yellow";
    a [label="Foo"];
    b [shape=circle];
    a -- b -- c [color=blue];
}"""

graphs = pydot.graph_from_dot_data(dot_string)
graph = graphs[0]

Create a graph from scratch using pydot objects

This is where the cool stuff starts. Use this method if you want to build new graphs with Python code.

import pydot

graph = pydot.Dot("my_graph", graph_type="graph", bgcolor="yellow")

# Add nodes
my_node = pydot.Node("a", label="Foo")
graph.add_node(my_node)
# Or, without using an intermediate variable:
graph.add_node(pydot.Node("b", shape="circle"))

# Add edges
my_edge = pydot.Edge("a", "b", color="blue")
graph.add_edge(my_edge)
# Or, without using an intermediate variable:
graph.add_edge(pydot.Edge("b", "c", color="blue"))

You can use these basic building blocks in your Python program to dynamically generate a graph. For example, start with a basic pydot.Dot graph object, then loop through your data as you add nodes and edges. Use values from your data as labels to determine shapes, edges and so on. This allows you to easily create visualizations of thousands of related objects.

Convert a NetworkX graph to a pydot graph

NetworkX has conversion methods for pydot graphs:

import networkx
import pydot

# See NetworkX documentation on how to build a NetworkX graph.
graph = networkx.drawing.nx_pydot.to_pydot(my_networkx_graph)

2. Edit

You can now further manipulate your graph using pydot methods:

Add more nodes and edges:

graph.add_edge(pydot.Edge("b", "d", style="dotted"))

Edit attributes of graphs, nodes and edges:

graph.set_bgcolor("lightyellow")
graph.get_node("b")[0].set_shape("box")

3. Output

Here are three different output options:

Generate an image

If you just want to save the image to a file, use one of the write_* methods:

graph.write_png("output.png")

If you need to further process the image output, the create_* methods will get you a Python bytes object:

output_graphviz_svg = graph.create_svg()

Retrieve the DOT string

There are two different DOT strings you can retrieve:

  • The "raw" pydot DOT: This is generated the fastest and will usually still look quite similar to the DOT you put in. It is generated by pydot itself, without calling Graphviz.

    # As a string:
    output_raw_dot = graph.to_string()
    # Or, save it as a DOT-file:
    graph.write_raw("output_raw.dot")
    
  • The Graphviz DOT: You can use it to check how Graphviz lays out the graph before it produces an image. It is generated by Graphviz.

    # As a bytes literal:
    output_graphviz_dot = graph.create_dot()
    # Or, save it as a DOT-file:
    graph.write_dot("output_graphviz.dot")
    

Convert to a NetworkX graph

NetworkX has a conversion method for pydot graphs:

my_networkx_graph = networkx.drawing.nx_pydot.from_pydot(graph)

More help

For more help, see the docstrings of the various pydot objects and methods. For example, help(pydot), help(pydot.Graph) and help(pydot.Dot.write).

More documentation contributions welcome.

Troubleshooting

Image for: Troubleshooting

Enable logging

pydot uses Python's standard logging module. To see the logs, assuming logging has not been configured already:

>>> import logging
>>> logging.basicConfig(level=logging.DEBUG)
>>> import pydot
DEBUG:pydot:pydot initializing
DEBUG:pydot:pydot <version>
DEBUG:pydot.core:pydot core module initializing
DEBUG:pydot.dot_parser:pydot dot_parser module initializing

Warning: When DEBUG level logging is enabled, pydot may log the data that it processes, such as graph contents or DOT strings. This can cause the log to become very large or contain sensitive information.

Advanced logging configuration

  • Check out the Python logging documentation and the logging_tree visualizer.
  • pydot does not add any handlers to its loggers, nor does it setup or modify your root logger. The pydot loggers are created with the default level NOTSET.
  • pydot registers the following loggers:
    • pydot: Parent logger. Emits a few messages during startup.
    • pydot.core: Messages related to pydot objects, Graphviz execution and anything else not covered by the other loggers.
    • pydot.dot_parser: Messages related to the parsing of DOT strings.

License

Image for: License

Distributed under the MIT license.

The module pydot._vendor.tempfile is based on the Python 3.12 standard library module tempfile.py, Copyright © 2001-2023 Python Software Foundation. All rights reserved. Licensed under the terms of the Python-2.0 license.

Contacts

Image for: Contacts

Current maintainer(s):

  • Łukasz Łapiński <lukaszlapinski7 (at) gmail (dot) com>

Past maintainers:

  • Sebastian Kalinowski <sebastian (at) kalinowski (dot) eu> (GitHub: @prmtl)
  • Peter Nowee <peter (at) peternowee (dot) com> (GitHub: @peternowee)

Original author: Ero Carrera <ero (dot) carrera (at) gmail (dot) com>

Project details

Image for: Project details

Download files

Image for: Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydot-4.0.0.tar.gz (161.8 kB view details)

Uploaded Source

Built Distribution

pydot-4.0.0-py3-none-any.whl (37.5 kB view details)

Uploaded Python 3

File details

Image for: File details

Details for the file pydot-4.0.0.tar.gz.

File metadata

  • Download URL: pydot-4.0.0.tar.gz
  • Upload date:
  • Size: 161.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for pydot-4.0.0.tar.gz
Algorithm Hash digest
SHA256 12f16493337cade2f7631b87c8ccd299ba2e251f3ee5d0732a058df2887afe97
MD5 dde6bc4386285a44f9f072f226e08460
BLAKE2b-256 d1c36034ed1ebf2e3ba95a0e35fa7c43104e40444c0ed2b5325702c63e824dbf

See more details on using hashes here.

File details

Image for: File details

Details for the file pydot-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: pydot-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 37.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for pydot-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cf86e13a6cfe2a96758a9702537f77e0ac1368db8ef277b4d3b34473ea425c97
MD5 222dde5fedffb4ddb3e836505a88b16b
BLAKE2b-256 0a16984c0cf5073a23154b1f95c9d131b14c9fea83bfadae4ba8fc169daded11

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page