A RetroSearch Logo

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

Search Query:

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

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

pydot:

and networkx can convert its graphs to pydot.

Development occurs at GitHub, where you can report issues and contribute code.

The examples here will show you the most common input, editing and output methods.

No matter what you want to do with pydot, it will need some input to start with. Here are 3 common options:

  1. Import a graph from an existing DOT-file.

    Use this method if you already have a DOT-file describing a graph, for example as output of another program. Let's say you already have this example.dot (based on an example from Wikipedia):

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

    Just read the graph from the DOT-file:

    import pydot
    
    graphs = pydot.graph_from_dot_file("example.dot")
    graph = graphs[0]
  2. or: Parse a graph from an existing DOT-string.

    Use this method if you already have a DOT-string describing a graph in a Python variable:

    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]
  3. or: Create a graph from scratch using pydot objects.

    Now this is where the cool stuff starts. Use this method if you want to build new graphs from Python.

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

    Imagine using these basic building blocks from your Python program to dynamically generate a graph. For example, start out with a basic pydot.Dot graph object, then loop through your data while adding nodes and edges. Use values from your data as labels, to determine shapes, edges and so forth. This way, you can easily build visualizations of thousands of interconnected items.

  4. or: 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:

Here are 3 different output options:

  1. Generate an image.

    To generate an image of the graph, use one of the create_*() or write_*() methods.

  2. Retrieve the DOT string.

    There are two different DOT strings you can retrieve:

  3. Convert to a NetworkX graph.

    Here as well, 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.

From PyPI using pip:

pip install pydot

From source:

python setup.py install

Distributed under an MIT license.

Maintainers:

Original author: Ero Carrera ero.carrera@gmail.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