Package: pydot
Aboutpydot:is an interface toGraphvizcan parse and dump into theDOT languageused by GraphViz,is written in pure Python,andnetworkxcan convert its graphs topydot.Development occurs atGitHub, where you can report issues and
contribute code.ExamplesThe examples here will show you the most common input, editing and
output methods.InputNo matter what you want to do withpydot, it will need some input to
start with. Here are 3 common options: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 thisexample.dot(based on anexample from Wikipedia):graphmy_graph{bgcolor="yellow";a[label="Foo"];b[shape=circle];a--b--c[color=blue];}Just read the graph from the DOT-file:importpydotgraphs=pydot.graph_from_dot_file("example.dot")graph=graphs[0]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:importpydotdot_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]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.importpydotgraph=pydot.Dot("my_graph",graph_type="graph",bgcolor="yellow")# Add nodesmy_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 edgesmy_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
basicpydot.Dotgraph 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.or: Convert a NetworkX graph to a pydot graph.NetworkX has conversion methods for pydot graphs:importnetworkximportpydot# See NetworkX documentation on how to build a NetworkX graph.graph=networkx.drawing.nx_pydot.to_pydot(my_networkx_graph)EditYou can now further manipulate your graph using pydot methods:Add further nodes and edges:graph.add_edge(pydot.Edge("b","d",style="dotted"))Edit attributes of graph, nodes and edges:graph.set_bgcolor("lightyellow")graph.get_node("b")[0].set_shape("box")OutputHere are 3 different output options:Generate an image.To generate an image of the graph, use one of thecreate_*()orwrite_*()methods.If you need to further process the output in Python, thecreate_*methods will get you a Python bytes object:output_graphviz_svg=graph.create_svg()If instead you just want to save the image to a file, use one of
thewrite_*methods:graph.write_png("output.png")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.Here as well, NetworkX has a conversion method for pydot graphs:my_networkx_graph=networkx.drawing.nx_pydot.from_pydot(graph)More helpFor more help, see the docstrings of the various pydot objects and
methods. For example,help(pydot),help(pydot.Graph)andhelp(pydot.Dot.write).Moredocumentation contributions welcome.InstallationFromPyPIusingpip:pip install pydotFrom source:python setup.py installDependenciespyparsing: used only forloadingDOT files,
installed automatically duringpydotinstallation.GraphViz: used to render graphs as PDF, PNG, SVG, etc.
Should be installed separately, using your system'spackage manager, something similar (e.g.,MacPorts),
or fromits source.LicenseDistributed under anMIT license.ContactsMaintainers:Sebastian Kalinowskisebastian@kalinowski.eu(GitHub: @prmtl)Peter Noweepeter@peternowee.com(GitHub: @peternowee)Original author: Ero Carreraero.carrera@gmail.com