AGraph Class¶
-
class
AGraph
(thing=None, filename=None, data=None, string=None, handle=None, name='', strict=True, directed=False, **attr)¶ 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
-
acyclic
(args='', copy=False)¶ 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_cycle
(nlist)¶ Add the cycle of nodes given in nlist.
-
add_edge
(u, v=None, key=None, **attr)¶ 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('a','b',color='green')
Attributes must be valid strings.
See http://www.graphviz.org/doc/info/attrs.html for a list of attributes.
-
add_edges_from
(ebunch, **attr)¶ 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_node
(n, **attr)¶ 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
(nbunch, **attr)¶ 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_path
(nlist)¶ Add the path of nodes given in nlist.
-
add_subgraph
(nbunch=None, name=None, **attr)¶ Return subgraph induced by nodes in nbunch.
-
clear
()¶ Remove all nodes, edges, and attributes from the graph.
-
close
()¶
-
copy
()¶ Return a copy of the graph.
-
degree
(nbunch=None, with_labels=False)¶ 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.
-
degree_iter
(nbunch=None, indeg=True, outdeg=True)¶ Return an iterator over the degree of the nodes given in nbunch container.
Returns paris of (node,degree).
-
delete_edge
(u, v=None, key=None)¶ Remove edge between nodes u and v from the graph.
With optional key argument will only remove an edge matching (u,v,key).
-
delete_edges_from
(ebunch)¶ Remove edges from ebunch (a container of edges).
-
delete_node
(n)¶ 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')
-
delete_nodes_from
(nbunch)¶ 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)
-
delete_subgraph
(name)¶ Remove subgraph with given name.
-
directed
¶ Return True if graph is directed or False if not.
-
draw
(path=None, format=None, prog=None, args='')¶ 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.
-
edges
(nbunch=None, keys=False)¶ 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')]
-
edges_iter
(nbunch=None, keys=False)¶ Return iterator over edges in the graph.
If the optional nbunch (container of nodes) only edges adjacent to nodes in nbunch will be returned.
Note: modifying the graph structure while iterating over edges may produce unpredictable results. Use edges() as an alternative.
-
from_string
(string)¶ 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
-
get_edge
(u, v, key=None)¶ 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).
-
get_name
()¶
-
get_node
(n)¶ Return a node object (Node) corresponding to node n.
>>> G=AGraph() >>> G.add_node('a') >>> node=G.get_node('a') >>> print(node) a
-
get_subgraph
(name)¶ Return existing subgraph with specified name or None if it doesn’t exist.
-
has_edge
(u, v=None, key=None)¶ 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).
-
has_neighbor
(u, v, key=None)¶ 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).
-
has_node
(n)¶ 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
-
in_degree
(nbunch=None, with_labels=False)¶ 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.
-
in_degree_iter
(nbunch=None)¶ Return an iterator over the in-degree of the nodes given in nbunch container.
Returns paris of (node,degree).
-
in_edges
(nbunch=None, keys=False)¶ 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.
-
in_edges_iter
(nbunch=None, keys=False)¶ 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.
-
in_neighbors
(n)¶ Return list of predecessor nodes of n.
-
is_directed
()¶ Return True if graph is directed or False if not.
-
is_strict
()¶ Return True if graph is strict or False if not.
Strict graphs do not allow parallel edges or self loops.
-
is_undirected
()¶ Return True if graph is undirected or False if not.
-
iterdegree
(nbunch=None, indeg=True, outdeg=True)¶ Return an iterator over the degree of the nodes given in nbunch container.
Returns paris of (node,degree).
-
iteredges
(nbunch=None, keys=False)¶ Return iterator over edges in the graph.
If the optional nbunch (container of nodes) only edges adjacent to nodes in nbunch will be returned.
Note: modifying the graph structure while iterating over edges may produce unpredictable results. Use edges() as an alternative.
-
iterindegree
(nbunch=None)¶ Return an iterator over the in-degree of the nodes given in nbunch container.
Returns paris of (node,degree).
-
iterinedges
(nbunch=None, keys=False)¶ 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.
-
iterneighbors
(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.
-
iternodes
()¶ 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.
-
iteroutdegree
(nbunch=None)¶ Return an iterator over the out-degree of the nodes given in nbunch container.
Returns paris of (node,degree).
-
iteroutedges
(nbunch=None, keys=False)¶ 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.
-
iterpred
(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.
-
itersucc
(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.
-
layout
(prog='neato', args='')¶ 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.
-
name
¶
-
neighbors
(n)¶ Return a list of the nodes attached to n.
-
neighbors_iter
(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.
-
nodes
()¶ Return a list of all nodes in the graph.
-
nodes_iter
()¶ 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.
-
number_of_edges
()¶ Return the number of edges in the graph.
-
number_of_nodes
()¶ Return the number of nodes in the graph.
-
order
()¶ Return the number of nodes in the graph.
-
out_degree
(nbunch=None, with_labels=False)¶ 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.
-
out_degree_iter
(nbunch=None)¶ Return an iterator over the out-degree of the nodes given in nbunch container.
Returns paris of (node,degree).
-
out_edges
(nbunch=None, keys=False)¶ 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.
-
out_edges_iter
(nbunch=None, keys=False)¶ 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.
-
out_neighbors
(n)¶ Return list of successor nodes of n.
-
predecessors
(n)¶ Return list of predecessor nodes of n.
-
predecessors_iter
(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.
-
prepare_nbunch
(nbunch=None)¶
-
read
(path)¶ Read graph from dot format file on path.
path can be a file name or file handle
use:
G.read('file.dot')
-
remove_edge
(u, v=None, key=None)¶ 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)¶ Remove edges from ebunch (a container of edges).
-
remove_node
(n)¶ 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
(nbunch)¶ 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
(name)¶ Remove subgraph with given name.
-
reverse
()¶ Return copy of directed graph with edge directions reversed.
-
size
()¶ Return the number of edges in the graph.
-
strict
¶ Return True if graph is strict or False if not.
Strict graphs do not allow parallel edges or self loops.
-
string
()¶ Return a string (unicode) representation of graph in dot format.
-
string_nop
()¶ Return a string (unicode) representation of graph in dot format.
-
subgraph
(nbunch=None, name=None, **attr)¶ Return subgraph induced by nodes in nbunch.
-
subgraph_parent
(nbunch=None, name=None)¶ Return parent graph of subgraph or None if graph is root graph.
-
subgraph_root
(nbunch=None, name=None)¶ Return root graph of subgraph or None if graph is root graph.
-
subgraphs
()¶ Return a list of all subgraphs in the graph.
-
subgraphs_iter
()¶ Iterator over subgraphs.
-
successors
(n)¶ Return list of successor nodes of n.
-
successors_iter
(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.
-
to_directed
(**kwds)¶ Return directed copy of graph.
Each undirected edge u-v is represented as two directed edges u->v and v->u.
-
to_string
()¶ Return a string (unicode) representation of graph in dot format.
-
to_undirected
()¶ Return undirected copy of graph.
-
tred
(args='', copy=False)¶ 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
(path=None)¶ Write graph in dot format to file on path.
path can be a file name or file handle
use:
G.write('file.dot')
-