Class for Graphviz agraph type.
Example use
>>> from pygraphviz import *
>>> G=AGraph()
>>> G=AGraph(directed=True)
>>> G=AGraph("file.dot")
Graphviz graph keyword parameters are processed so you may add them like
>>> G=AGraph(landscape='true',ranksep='0.1')
or alternatively
>>> G=AGraph()
>>> G.graph_attr.update(landscape='true',ranksep='0.1')
and
>>> G.node_attr.update(color='red')
>>> G.edge_attr.update(len='2.0',color='blue')
See http://www.graphviz.org/doc/info/attrs.html for a list of attributes.
Keyword parameters:
thing is a generic input type (filename, string, handle to pointer, dictionary of dictionaries). An attempt is made to automaticaly detect the type so you may write for example:
>>> d={'1': {'2': None}, '2': {'1': None, '3': None}, '3': {'2': None}}
>>> A=AGraph(d)
>>> s=A.to_string()
>>> B=AGraph(s)
>>> h=B.handle
>>> C=AGraph(h)
Parameters:
name: Name for the graph
strict: True|False (True for simple graphs)
directed: True|False
data: Dictionary of dictionaries or dictionary of lists
representing nodes or edges to load into intial graph
string: String containing a dot format graph
handle: Swig pointer to an agraph_t data structure
Reverse sufficient edges in digraph to make graph acyclic. Modifies existing graph.
To create a new graph use
>>> A=AGraph()
>>> B=A.acyclic(copy=True)
See the graphviz “acyclic” program for details of the algorithm.
Add the cycle of nodes given in nlist.
Add a single edge between nodes u and v.
If the nodes u and v are not in the graph they will added.
If u and v are not strings, conversion to a string will be attempted. String conversion will work if u and v have valid string representation (try str(u) if you are unsure).
>>> G=AGraph()
>>> G.add_edge('a','b')
>>> G.edges()
[(u'a', u'b')]
The optional key argument allows assignment of a key to the edge. This is especially useful to distinguish between parallel edges in multi-edge graphs (strict=False).
>>> G=AGraph(strict=False)
>>> G.add_edge('a','b','first')
>>> G.add_edge('a','b','second')
>>> sorted(G.edges(keys=True))
[(u'a', u'b', u'first'), (u'a', u'b', u'second')]
Attributes can be added when edges are created
>>> G.add_edge(u'a',u'b',color='green')
Attributes must be valid strings.
See http://www.graphviz.org/doc/info/attrs.html for a list of attributes.
Add nodes to graph from a container ebunch.
ebunch is a container of edges such as a list or dictionary.
>>> G=AGraph()
>>> elist=[('a','b'),('b','c')]
>>> G.add_edges_from(elist)
Attributes can be added when edges are created
>>> G.add_edges_from(elist, color='green')
Add a single node n.
If n is not a string, conversion to a string will be attempted. String conversion will work if n has valid string representation (try str(n) if you are unsure).
>>> G=AGraph()
>>> G.add_node('a')
>>> G.nodes()
[u'a']
>>> G.add_node(1) # will be converted to a string
>>> G.nodes()
[u'a', u'1']
Attributes can be added to nodes on creation (attribute values must be strings)
>>> G.add_node(2,color='red')
See http://www.graphviz.org/doc/info/attrs.html for a list of attributes.
Anonymous Graphviz nodes are currently not implemented.
Add nodes from a container nbunch.
nbunch can be any iterable container such as a list or dictionary
>>> G=AGraph()
>>> nlist=['a','b',1,'spam']
>>> G.add_nodes_from(nlist)
>>> sorted(G.nodes())
[u'1', u'a', u'b', u'spam']
Attributes can be added to nodes on creation
>>> G.add_nodes_from(nlist, color='red') # set all nodes in nlist red
Add the path of nodes given in nlist.
Return subgraph induced by nodes in nbunch.
Remove all nodes, edges, and attributes from the graph.
Return a copy of the graph.
Return the degree of nodes given in nbunch container.
Using optional with_labels=True returns a dictionary keyed by node with value set to the degree.
Return an iterator over the degree of the nodes given in nbunch container.
Returns paris of (node,degree).
Remove edge between nodes u and v from the graph.
With optional key argument will only remove an edge matching (u,v,key).
Remove edges from ebunch (a container of edges).
Remove the single node n.
Attempting to remove a node that isn’t in the graph will produce an error.
>>> G=AGraph()
>>> G.add_node('a')
>>> G.remove_node('a')
Remove nodes from a container nbunch.
nbunch can be any iterable container such as a list or dictionary
>>> G=AGraph()
>>> nlist=['a','b',1,'spam']
>>> G.add_nodes_from(nlist)
>>> G.remove_nodes_from(nlist)
Remove subgraph with given name.
Return True if graph is directed or False if not.
Output graph to path in specified format.
An attempt will be made to guess the output format based on the file extension of path. If that fails, then the format parameter will be used.
Note, if path is a file object returned by a call to os.fdopen(), then the method for discovering the format will not work. In such cases, one should explicitly set the format parameter; otherwise, it will default to ‘dot’.
Formats (not all may be available on every system depending on how Graphviz was built)
‘canon’, ‘cmap’, ‘cmapx’, ‘cmapx_np’, ‘dia’, ‘dot’, ‘fig’, ‘gd’, ‘gd2’, ‘gif’, ‘hpgl’, ‘imap’, ‘imap_np’, ‘ismap’, ‘jpe’, ‘jpeg’, ‘jpg’, ‘mif’, ‘mp’, ‘pcl’, ‘pdf’, ‘pic’, ‘plain’, ‘plain-ext’, ‘png’, ‘ps’, ‘ps2’, ‘svg’, ‘svgz’, ‘vml’, ‘vmlz’, ‘vrml’, ‘vtx’, ‘wbmp’, ‘xdot’, ‘xlib’
If prog is not specified and the graph has positions (see layout()) then no additional graph positioning will be performed.
Optional prog=[‘neato’|’dot’|’twopi’|’circo’|’fdp’|’nop’] will use specified graphviz layout method.
>>> G=AGraph()
>>> G.layout()
# use current node positions, output ps in ‘file.ps’ >>> G.draw(‘file.ps’)
# use dot to position, output png in ‘file’ >>> G.draw(‘file’, format=’png’,prog=’dot’)
# use keyword ‘args’ to pass additional arguments to graphviz >>> G.draw(‘test.ps’,prog=’twopi’,args=’-Gepsilon=1’)
The layout might take a long time on large graphs.
Return list of edges in the graph.
If the optional nbunch (container of nodes) only edges adjacent to nodes in nbunch will be returned.
>>> G=AGraph()
>>> G.add_edge('a','b')
>>> G.add_edge('c','d')
>>> print sorted(G.edges())
[(u'a', u'b'), (u'c', u'd')]
>>> print G.edges('a')
[(u'a', u'b')]
Return iterator over out edges in the graph.
If the optional nbunch (container of nodes) only out edges adjacent to nodes in nbunch will be returned.
Note: modifying the graph structure while iterating over edges may produce unpredictable results. Use out_edges() as an alternative.
Load a graph from a string in dot format.
Overwrites any existing graph.
To make a new graph from a string use
>>> s='digraph {1 -> 2}'
>>> A=AGraph()
>>> t=A.from_string(s)
>>> A=AGraph(string=s) # specify s is a string
>>> A=AGraph(s) # s assumed to be a string during initialization
Return an edge object (Edge) corresponding to edge (u,v).
>>> G=AGraph()
>>> G.add_edge('a','b')
>>> edge=G.get_edge('a','b')
>>> print edge
(u'a', u'b')
With optional key argument will only get edge matching (u,v,key).
Return a node object (Node) corresponding to node n.
>>> G=AGraph()
>>> G.add_node('a')
>>> node=G.get_node('a')
>>> print node
a
Return existing subgraph with specified name or None if it doesn’t exist.
Return True an edge u-v is in the graph or False if not.
>>> G=AGraph()
>>> G.add_edge('a','b')
>>> G.has_edge('a','b')
True
Optional key argument will restrict match to edges (u,v,key).
Return True if u has an edge to v or False if not.
>>> G=AGraph()
>>> G.add_edge('a','b')
>>> G.has_neighbor('a','b')
True
Optional key argument will only find edges (u,v,key).
Return True if n is in the graph or False if not.
>>> G=AGraph()
>>> G.add_node('a')
>>> G.has_node('a')
True
>>> 'a' in G # same as G.has_node('a')
True
Return the in-degree of nodes given in nbunch container.
Using optional with_labels=True returns a dictionary keyed by node with value set to the degree.
Return an iterator over the in-degree of the nodes given in nbunch container.
Returns paris of (node,degree).
Return list of in edges in the graph. If the optional nbunch (container of nodes) only in edges adjacent to nodes in nbunch will be returned.
Return iterator over out edges in the graph.
If the optional nbunch (container of nodes) only out edges adjacent to nodes in nbunch will be returned.
Note: modifying the graph structure while iterating over edges may produce unpredictable results. Use in_edges() as an alternative.
Return list of predecessor nodes of n.
Return True if graph is directed or False if not.
Return True if graph is strict or False if not.
Strict graphs do not allow parallel edges or self loops.
Return True if graph is undirected or False if not.
Return an iterator over the degree of the nodes given in nbunch container.
Returns paris of (node,degree).
Return iterator over out edges in the graph.
If the optional nbunch (container of nodes) only out edges adjacent to nodes in nbunch will be returned.
Note: modifying the graph structure while iterating over edges may produce unpredictable results. Use out_edges() as an alternative.
Return an iterator over the in-degree of the nodes given in nbunch container.
Returns paris of (node,degree).
Return iterator over out edges in the graph.
If the optional nbunch (container of nodes) only out edges adjacent to nodes in nbunch will be returned.
Note: modifying the graph structure while iterating over edges may produce unpredictable results. Use in_edges() as an alternative.
Return iterator over the nodes attached to n.
Note: modifying the graph structure while iterating over node neighbors may produce unpredictable results. Use neighbors() as an alternative.
Return an iterator over all the nodes in the graph.
Note: modifying the graph structure while iterating over the nodes may produce unpredictable results. Use nodes() as an alternative.
Return an iterator over the out-degree of the nodes given in nbunch container.
Returns paris of (node,degree).
Return iterator over out edges in the graph.
If the optional nbunch (container of nodes) only out edges adjacent to nodes in nbunch will be returned.
Note: modifying the graph structure while iterating over edges may produce unpredictable results. Use out_edges() as an alternative.
Return iterator over predecessor nodes of n.
Note: modifying the graph structure while iterating over node predecessors may produce unpredictable results. Use predecessors() as an alternative.
Return iterator over successor nodes of n.
Note: modifying the graph structure while iterating over node successors may produce unpredictable results. Use successors() as an alternative.
Assign positions to nodes in graph.
Optional prog=[‘neato’|’dot’|’twopi’|’circo’|’fdp’|’nop’] will use specified graphviz layout method.
>>> A=AGraph()
>>> A.layout() # uses neato
>>> A.layout(prog='dot')
Use keyword args to add additional arguments to graphviz programs.
The layout might take a long time on large graphs.
Return a list of the nodes attached to n.
Return iterator over the nodes attached to n.
Note: modifying the graph structure while iterating over node neighbors may produce unpredictable results. Use neighbors() as an alternative.
Return a list of all nodes in the graph.
Return an iterator over all the nodes in the graph.
Note: modifying the graph structure while iterating over the nodes may produce unpredictable results. Use nodes() as an alternative.
Return the number of edges in the graph.
Return the number of nodes in the graph.
Return the number of nodes in the graph.
Return the out-degree of nodes given in nbunch container.
Using optional with_labels=True returns a dictionary keyed by node with value set to the degree.
Return an iterator over the out-degree of the nodes given in nbunch container.
Returns paris of (node,degree).
Return list of out edges in the graph.
If the optional nbunch (container of nodes) only out edges adjacent to nodes in nbunch will be returned.
Return iterator over out edges in the graph.
If the optional nbunch (container of nodes) only out edges adjacent to nodes in nbunch will be returned.
Note: modifying the graph structure while iterating over edges may produce unpredictable results. Use out_edges() as an alternative.
Return list of successor nodes of n.
Return list of predecessor nodes of n.
Return iterator over predecessor nodes of n.
Note: modifying the graph structure while iterating over node predecessors may produce unpredictable results. Use predecessors() as an alternative.
Read graph from dot format file on path.
path can be a file name or file handle
use:
G.read('file.dot')
Remove edge between nodes u and v from the graph.
With optional key argument will only remove an edge matching (u,v,key).
Remove edges from ebunch (a container of edges).
Remove the single node n.
Attempting to remove a node that isn’t in the graph will produce an error.
>>> G=AGraph()
>>> G.add_node('a')
>>> G.remove_node('a')
Remove nodes from a container nbunch.
nbunch can be any iterable container such as a list or dictionary
>>> G=AGraph()
>>> nlist=['a','b',1,'spam']
>>> G.add_nodes_from(nlist)
>>> G.remove_nodes_from(nlist)
Remove subgraph with given name.
Return copy of directed graph with edge directions reversed.
Return the number of edges in the graph.
Return True if graph is strict or False if not.
Strict graphs do not allow parallel edges or self loops.
Return a string (unicode) represetnation of graph in dot format.
Return a string (unicode) representation of graph in dot format.
Return subgraph induced by nodes in nbunch.
Return parent graph of subgraph or None if graph is root graph.
Return root graph of subgraph or None if graph is root graph.
Return a list of all subgraphs in the graph.
Iterator over subgraphs.
Return list of successor nodes of n.
Return iterator over successor nodes of n.
Note: modifying the graph structure while iterating over node successors may produce unpredictable results. Use successors() as an alternative.
Return directed copy of graph.
Each undirected edge u-v is represented as two directed edges u->v and v->u.
Return a string (unicode) representation of graph in dot format.
Return undirected copy of graph.
Transitive reduction of graph. Modifies existing graph.
To create a new graph use
>>> A=AGraph()
>>> B=A.tred(copy=True)
See the graphviz “tred” program for details of the algorithm.
Write graph in dot format to file on path.
path can be a file name or file handle
use:
G.write('file.dot')