dot.rst 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. :mod:`altgraph.Dot` --- Interface to the dot language
  2. =====================================================
  3. .. module:: altgraph.Dot
  4. :synopsis: Interface to the dot language as used by Graphviz..
  5. The :py:mod:`~altgraph.Dot` module provides a simple interface to the
  6. file format used in the `graphviz`_ program. The module is intended to
  7. offload the most tedious part of the process (the **dot** file generation)
  8. while transparently exposing most of its features.
  9. .. _`graphviz`: <http://www.research.att.com/sw/tools/graphviz/>`_
  10. To display the graphs or to generate image files the `graphviz`_
  11. package needs to be installed on the system, moreover the :command:`dot` and :command:`dotty` programs must
  12. be accesible in the program path so that they can be ran from processes spawned
  13. within the module.
  14. Example usage
  15. -------------
  16. Here is a typical usage::
  17. from altgraph import Graph, Dot
  18. # create a graph
  19. edges = [ (1,2), (1,3), (3,4), (3,5), (4,5), (5,4) ]
  20. graph = Graph.Graph(edges)
  21. # create a dot representation of the graph
  22. dot = Dot.Dot(graph)
  23. # display the graph
  24. dot.display()
  25. # save the dot representation into the mydot.dot file
  26. dot.save_dot(file_name='mydot.dot')
  27. # save dot file as gif image into the graph.gif file
  28. dot.save_img(file_name='graph', file_type='gif')
  29. Directed graph and non-directed graph
  30. -------------------------------------
  31. Dot class can use for both directed graph and non-directed graph
  32. by passing *graphtype* parameter.
  33. Example::
  34. # create directed graph(default)
  35. dot = Dot.Dot(graph, graphtype="digraph")
  36. # create non-directed graph
  37. dot = Dot.Dot(graph, graphtype="graph")
  38. Customizing the output
  39. ----------------------
  40. The graph drawing process may be customized by passing
  41. valid :command:`dot` parameters for the nodes and edges. For a list of all
  42. parameters see the `graphviz`_ documentation.
  43. Example::
  44. # customizing the way the overall graph is drawn
  45. dot.style(size='10,10', rankdir='RL', page='5, 5' , ranksep=0.75)
  46. # customizing node drawing
  47. dot.node_style(1, label='BASE_NODE',shape='box', color='blue' )
  48. dot.node_style(2, style='filled', fillcolor='red')
  49. # customizing edge drawing
  50. dot.edge_style(1, 2, style='dotted')
  51. dot.edge_style(3, 5, arrowhead='dot', label='binds', labelangle='90')
  52. dot.edge_style(4, 5, arrowsize=2, style='bold')
  53. .. note::
  54. dotty (invoked via :py:func:`~altgraph.Dot.display`) may not be able to
  55. display all graphics styles. To verify the output save it to an image
  56. file and look at it that way.
  57. Valid attributes
  58. ----------------
  59. - dot styles, passed via the :py:meth:`Dot.style` method::
  60. rankdir = 'LR' (draws the graph horizontally, left to right)
  61. ranksep = number (rank separation in inches)
  62. - node attributes, passed via the :py:meth:`Dot.node_style` method::
  63. style = 'filled' | 'invisible' | 'diagonals' | 'rounded'
  64. shape = 'box' | 'ellipse' | 'circle' | 'point' | 'triangle'
  65. - edge attributes, passed via the :py:meth:`Dot.edge_style` method::
  66. style = 'dashed' | 'dotted' | 'solid' | 'invis' | 'bold'
  67. arrowhead = 'box' | 'crow' | 'diamond' | 'dot' | 'inv' | 'none' | 'tee' | 'vee'
  68. weight = number (the larger the number the closer the nodes will be)
  69. - valid `graphviz colors <http://www.research.att.com/~erg/graphviz/info/colors.html>`_
  70. - for more details on how to control the graph drawing process see the
  71. `graphviz reference <http://www.research.att.com/sw/tools/graphviz/refs.html>`_.
  72. Class interface
  73. ---------------
  74. .. class:: Dot(graph[, nodes[, edgefn[, nodevisitor[, edgevisitor[, name[, dot[, dotty[, neato[, graphtype]]]]]]]]])
  75. Creates a new Dot generator based on the specified
  76. :class:`Graph <altgraph.Graph.Graph>`. The Dot generator won't reference
  77. the *graph* once it is constructed.
  78. If the *nodes* argument is present it is the list of nodes to include
  79. in the graph, otherwise all nodes in *graph* are included.
  80. If the *edgefn* argument is present it is a function that yields the
  81. nodes connected to another node, this defaults to
  82. :meth:`graph.out_nbr <altgraph.Graph.Graph.out_nbr>`. The constructor won't
  83. add edges to the dot file unless both the head and tail of the edge
  84. are in *nodes*.
  85. If the *name* is present it specifies the name of the graph in the resulting
  86. dot file. The default is ``"G"``.
  87. The functions *nodevisitor* and *edgevisitor* return the default style
  88. for a given edge or node (both default to functions that return an empty
  89. style).
  90. The arguments *dot*, *dotty* and *neato* are used to pass the path to
  91. the corresponding `graphviz`_ command.
  92. Updating graph attributes
  93. .........................
  94. .. method:: Dot.style(\**attr)
  95. Sets the overall style (graph attributes) to the given attributes.
  96. See `Valid Attributes`_ for more information about the attributes.
  97. .. method:: Dot.node_style(node, \**attr)
  98. Sets the style for *node* to the given attributes.
  99. This method will add *node* to the graph when it isn't already
  100. present.
  101. See `Valid Attributes`_ for more information about the attributes.
  102. .. method:: Dot.all_node_style(\**attr)
  103. Replaces the current style for all nodes
  104. .. method:: edge_style(head, tail, \**attr)
  105. Sets the style of an edge to the given attributes. The edge will
  106. be added to the graph when it isn't already present, but *head*
  107. and *tail* must both be valid nodes.
  108. See `Valid Attributes`_ for more information about the attributes.
  109. Emitting output
  110. ...............
  111. .. method:: Dot.display([mode])
  112. Displays the current graph via dotty.
  113. If the *mode* is ``"neato"`` the dot file is processed with
  114. the neato command before displaying.
  115. This method won't return until the dotty command exits.
  116. .. method:: save_dot(filename)
  117. Saves the current graph representation into the given file.
  118. .. note::
  119. For backward compatibility reasons this method can also
  120. be called without an argument, it will then write the graph
  121. into a fixed filename (present in the attribute :data:`Graph.temp_dot`).
  122. This feature is deprecated and should not be used.
  123. .. method:: save_image(file_name[, file_type[, mode]])
  124. Saves the current graph representation as an image file. The output
  125. is written into a file whose basename is *file_name* and whose suffix
  126. is *file_type*.
  127. The *file_type* specifies the type of file to write, the default
  128. is ``"gif"``.
  129. If the *mode* is ``"neato"`` the dot file is processed with
  130. the neato command before displaying.
  131. .. note::
  132. For backward compatibility reasons this method can also
  133. be called without an argument, it will then write the graph
  134. with a fixed basename (``"out"``).
  135. This feature is deprecated and should not be used.
  136. .. method:: iterdot()
  137. Yields all lines of a `graphviz`_ input file (including line endings).
  138. .. method:: __iter__()
  139. Alias for the :meth:`iterdot` method.