A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/juusomer/pydot below:

juusomer/pydot: Python interface to Graphviz's Dot language

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

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

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)

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")

Here are three different output options:

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()

There are two different DOT strings you can retrieve:

Convert to a NetworkX graph

NetworkX has a conversion method for pydot graphs:

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

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.

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

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.

Current maintainer(s):

Past maintainers:

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4