Module pygraphlib.pydot
Interface to the dot language
The pydot module provides a simple interface to the file
format used in the graphviz
program. The Dot
class of this module is intended to
offload the most tedious part of the process (the dot file
generation) while transparently exposing most of its features.
In order to display graphs or to generate image files the graphviz
package needs to be installed on the system, moreover the
dot
and dotty
programs must be accesible in
the program path so that they can be ran from processes spawned within
the module. See the Dot
documentation for further information
on the setup.
Example usage
>>> from pygraphlib import pygraph, pydot
>>> edges = [ (1,2), (2,3), (3,4), (3,5), (4,5), (5,1) ]
>>> graph = pygraph.from_list(edges)
>>> print graph
UGraph: 5 nodes, 6 edges
>>> dot = pydot.Dot(graph)
>>> dot.save_image('pydot_example1.gif', mode='gif')
Customizing the output
The graph drawing process may be customized by passing valid
dot parameters for the nodes and edges.
>>> dot = pydot.Dot(graph)
>>>
>>> dot.set_style(size='10,10', rankdir='RL', page='20, 20' , ranksep=0.75)
>>>
>>> dot.set_node_style(1, label='BASE NODE',shape='box', color='blue' )
>>> dot.set_node_style(2, style='filled', fillcolor='red')
>>>
>>> dot.set_edge_style(1, 2, style='dotted')
>>> dot.set_edge_style(3, 5, arrowhead='dot', label='binds', labelangle='90')
>>> dot.set_edge_style(4, 5, arrowsize=2, style='bold')
>>>
>>> dot.save_image('pydot_example2.gif', mode='gif')
Observation: dotty (invoked via Dot.show()
) may not be able to display
all graphics styles. To verify the output save it to an image file
and look at it that way.
Valid attributes
-
gobal dot attrbiutes, passed via the
Dot.set_style
method:
rankdir = 'LR' (draws the graph horizontally, left to right)
ranksep = number (rank separation in inches)
-
node attributes, passed via the
Dot.set_node_style
method:
style = 'filled' | 'invisible' | 'diagonals' | 'rounded'
shape = 'box' | 'ellipse' | 'circle' | 'point' | 'triangle'
-
edge attributes, passed via the
Dot.set_edge_style
method:
style = 'dashed' | 'dotted' | 'solid' | 'invis' | 'bold'
arrowhead = 'box' | 'crow' | 'diamond' | 'dot' | 'inv' | 'none' | 'tee' | 'vee'
weight = number (the larger the number the closer the nodes will be)
-
valid graphviz
colors
-
for a full list of attributes and other details on how to
precisely control the graph
drawing process see the graphviz manual.
Classes |
Dot |
A class that creates a graphviz (dot language) representation
of the graph. |