Logo Search packages:      
Sourcecode: python-networkx version File versions  Download package

def networkx::drawing::nx_pygraphviz::to_pygraphviz (   N,
  graph_attr = None,
  node_attr = None,
  edge_attr = None 
)

Creates a pygraphviz graph from an networkx graph N.

This simplest use creates a pygraphviz graph with no attributes,
just the nodes and edges. Attributes can be added by modifying
the resulting pygraphviz graph.
 
Optional arguments graph_attr, node_attr, edge_attr are used
to specify Graphviz graph properties to applied during the conversion.
http://www.graphviz.org/doc/info/attrs.html

The graph_attr, node_attr, and edge_attr  arguments can be either
a dictionary or function as follows:

graph_attr:
    dictionary with keys as attribute names and values as attribute values
    or
    function that returns a dictionary of attribute names and values
    The function takes one argument - the graph name::

      def graph_attr(G):
          a={'color':'red','label'='my graph'}
          return a
    
node_attr:
    dictionary keyed by node name that has as a value a dictionary
    of attribute names and values.
    or
    function that returns a dictionary of attribute names and values
    The function takes two arguments - the graph name and node name::

      def node_attr(G,n):
          a={'color':'red','shape'='diamond'}
          return a

edge_attr:
    dictionary keyed by edge tuple (u,v) that has as a value a dictionary
    of attribute names and values.
    or
    function that returns a dictionary of attribute names and values
    The function takes two arguments - the graph name and edge tuple e::

      def edge_attr(G,e):
          a={'color':'red','shape'='diamond'}
          return a

Definition at line 72 of file nx_pygraphviz.py.

                                 :
    """Creates a pygraphviz graph from an networkx graph N.

    This simplest use creates a pygraphviz graph with no attributes,
    just the nodes and edges. Attributes can be added by modifying
    the resulting pygraphviz graph.
 
    Optional arguments graph_attr, node_attr, edge_attr are used
    to specify Graphviz graph properties to applied during the conversion.
    http://www.graphviz.org/doc/info/attrs.html

    The graph_attr, node_attr, and edge_attr  arguments can be either
    a dictionary or function as follows:

    graph_attr:
        dictionary with keys as attribute names and values as attribute values
        or
        function that returns a dictionary of attribute names and values
        The function takes one argument - the graph name::

          def graph_attr(G):
              a={'color':'red','label'='my graph'}
              return a
        
    node_attr:
        dictionary keyed by node name that has as a value a dictionary
        of attribute names and values.
        or
        function that returns a dictionary of attribute names and values
        The function takes two arguments - the graph name and node name::

          def node_attr(G,n):
              a={'color':'red','shape'='diamond'}
              return a

    edge_attr:
        dictionary keyed by edge tuple (u,v) that has as a value a dictionary
        of attribute names and values.
        or
        function that returns a dictionary of attribute names and values
        The function takes two arguments - the graph name and edge tuple e::

          def edge_attr(G,e):
              a={'color':'red','shape'='diamond'}
              return a

    """

    if N.is_directed():
        A = pygraphviz.Agraph(name=N.name,
                              type=pygraphviz.graphviz.cvar.Agdirected)
        digraph=True
    else:
        A = pygraphviz.Agraph(name=N.name,
                              type=pygraphviz.graphviz.cvar.Agundirected)
        digraph=False

    # handle attributes, user dictionary or function
    if hasattr(graph_attr,"__getitem__"):   # if we are a dict
        get_graph_attr=graph_attr.copy   # call dict copy 
    else:
        get_graph_attr=graph_attr  # call user function

    # nodes            
    if hasattr(node_attr,"__getitem__"):   # if we are a dict
        get_node_attr=node_attr.__getitem__   # call as a function
    else:
        get_node_attr=node_attr  # call user function

    # edges
    if hasattr(edge_attr,"__getitem__"):   
        def get_edge_attr(e):
            return edge_attr[e]
    else: #    elif hasattr(edge_attr,"__call__"): 
        def get_edge_attr(e):
            return edge_attr(N,e)

    # graph attributes
    try:
        attr=get_graph_attr()
        A.set_attr(attr)
    except:
        pass

    # loop over nodes
    for n in N.nodes_iter():
        node=A.add_node(str(n))
        try:
            attr=get_node_attr(n)
            A.set_node_attr(node,attr)
        except:
            pass


    # loop over edges
    for e in N.edges_iter():
        name=None
        if len(e)==2:
            (u,v)=e
        elif len(e)==3:  # XGraph or XDigraph
            (u,v,x)=e
            if x is not None: 
                if is_string_like(x): # use data as edge name
                    name=x
        edge=A.add_edge(str(u),str(v),name)
        try:
            attr=get_edge_attr(e)
            A.set_edge_attr((u,v),attr)
        except:
            pass
        if not digraph:                    
            try:
                if len(e)==2:
                    er=(v,u)
                else:
                    er=(v,u,x)
                attr=get_edge_attr(er)
                A.set_edge_attr((v,u),attr)
            except:
                pass
    return A

def from_pygraphviz(A,


Generated by  Doxygen 1.6.0   Back to index