Ejemplo n.º 1
0
def pydot_layout(G, prog='neato'):
    """Create node positions using :mod:`pydot` and Graphviz.

    Parameters
    --------
    G : Graph
        NetworkX graph to be laid out.
    prog : optional[str]
        Basename of the GraphViz command with which to layout this graph.
        Defaults to `neato`, the default GraphViz command for undirected graphs.

    Returns
    --------
    dict
        Dictionary of positions keyed by node.

    """

    net = nx_to_dot(G)
    dot_file = pydot.graph_from_dot_data(net.create(prog=prog, format='dot'))
    net = dot_file[0]
    node_pos = {}
    for n in G.nodes():
        pydot_node = pydot.Node(n).get_name()
        node = net.get_node(pydot_node)

        if isinstance(node, list):
            node = node[0]
        pos = node.get_pos()[1:-1]  # strip leading and trailing double quotes
        if pos is not None:
            xx, yy = pos.split(",")
            node_pos[n] = (float(xx), float(yy))

    return node_pos
Ejemplo n.º 2
0
def draw_graphviz(network,
                  layout='dot',
                  image_format='png',
                  width=100,
                  save_name=None):
    """ Draws an


    Parameters
    ----------
    network : nx.DiGraph
    layout : str
        Which graphviz engine to use to layout graph
    image_format: str
        image format (png, svg, pdf)
    width : int
        Width of image
    save_name : str, optional
        If you want to export to file

    Returns
    -------

    """
    net_copy = network.copy()
    net_copy.graph.update({'K': '1'})
    net_copy.graph.update({'repulsiveforce ': '1'})
    net_copy.graph.update({'overlap ': 'false'})
    net_copy.graph.update({'splines ': 'true'})
    if save_name is not None:
        if save_name.endswith(image_format):
            out_name = save_name
        else:
            out_name = '{}.{}'.format(save_name, image_format)
        nx_to_dot(net_copy).write(out_name, format=image_format, prog=layout)
        if IPYTHON and run_from_ipython():
            display(Image(out_name, width=width))
    else:
        img = nx_to_dot(net_copy).create(format='png', prog=layout)
        if IPYTHON and run_from_ipython():
            display(Image(img, width=width))
Ejemplo n.º 3
0
def draw_graphviz(network, layout='dot', image_format='png', width=100,
                  save_name=None):
    """ Draws an


    Parameters
    ----------
    network : nx.DiGraph
    layout : str
        Which graphviz engine to use to layout graph
    image_format: str
        image format (png, svg, pdf)
    width : int
        Width of image
    save_name : str, optional
        If you want to export to file

    Returns
    -------

    """
    net_copy = network.copy()
    net_copy.graph.update({'K': '1'})
    net_copy.graph.update({'repulsiveforce ': '1'})
    net_copy.graph.update({'overlap ': 'false'})
    net_copy.graph.update({'splines ': 'true'})
    if save_name is not None:
        if save_name.endswith(image_format):
            out_name = save_name
        else:
            out_name = '{}.{}'.format(save_name, image_format)
        nx_to_dot(net_copy).write(out_name, format=image_format,
                                  prog=layout)
        if IPYTHON and run_from_ipython():
            display(Image(out_name, width=width))
    else:
        img = nx_to_dot(net_copy).create(format='png', prog=layout)
        if IPYTHON and run_from_ipython():
            display(Image(img, width=width))
Ejemplo n.º 4
0
def pydot_layout(G, prog='neato'):
    """Create node positions using :mod:`pydot` and Graphviz.

    Parameters
    --------
    G : Graph
        NetworkX graph to be laid out.
    prog : optional[str]
        Basename of the GraphViz command with which to layout this graph.
        Defaults to `neato`, the default GraphViz command for undirected graphs.

    Returns
    --------
    dict
        Dictionary of positions keyed by node.

    """
    tmp_g = G.copy()
    tmp_g.graph.update({'K': '1'})
    tmp_g.graph.update({'repulsiveforce ': '1'})
    tmp_g.graph.update({'overlap ': 'false'})
    tmp_g.graph.update({'splines ': 'true'})
    dot_fmt = nx_to_dot(tmp_g).create(prog=prog, format='dot', encoding='utf8')
    dot_fmt = unicode(dot_fmt, encoding=getpreferredencoding())
    net = pydot.graph_from_dot_data(dot_fmt)[0]
    node_pos = {}

    for n in tmp_g.nodes():
        pydot_node = pydot.Node(n).get_name()
        node = net.get_node(pydot_node)

        if isinstance(node, list):
            node = node[0]
        pos = node.get_pos()[1:-1]  # strip leading and trailing double quotes
        if pos is not None:
            xx, yy = pos.split(",")
            node_pos[n] = (float(xx), float(yy))

    return node_pos
Ejemplo n.º 5
0
def pydot_layout(G, prog='neato'):
    """Create node positions using :mod:`pydot` and Graphviz.

    Parameters
    --------
    G : Graph
        NetworkX graph to be laid out.
    prog : optional[str]
        Basename of the GraphViz command with which to layout this graph.
        Defaults to `neato`, the default GraphViz command for undirected graphs.

    Returns
    --------
    dict
        Dictionary of positions keyed by node.

    """
    tmp_g = G.copy()
    tmp_g.graph.update({'K': '1'})
    tmp_g.graph.update({'repulsiveforce ': '1'})
    tmp_g.graph.update({'overlap ': 'false'})
    tmp_g.graph.update({'splines ': 'true'})
    dot_fmt = nx_to_dot(tmp_g).create(prog=prog, format='dot', encoding='utf8')
    dot_fmt = unicode(dot_fmt, encoding=getpreferredencoding())
    net = pydot.graph_from_dot_data(dot_fmt)[0]
    node_pos = {}

    for n in tmp_g.nodes():
        pydot_node = pydot.Node(n).get_name()
        node = net.get_node(pydot_node)

        if isinstance(node, list):
            node = node[0]
        pos = node.get_pos()[1:-1]  # strip leading and trailing double quotes
        if pos is not None:
            xx, yy = pos.split(",")
            node_pos[n] = (float(xx), float(yy))

    return node_pos
Ejemplo n.º 6
0
def test_nx_to_dot():
    g = nx.DiGraph()
    g.add_edge('a', 'b')
    dot_g = exporters.nx_to_dot(g)
    assert isinstance(dot_g, pydotplus.Dot)
Ejemplo n.º 7
0
def test_nx_to_dot():
    g = nx.DiGraph()
    g.add_edge('a', 'b')
    dot_g = exporters.nx_to_dot(g)
    ok_(isinstance(dot_g, pydot.Dot))
Ejemplo n.º 8
0
def test_nx_to_dot():
    g = nx.DiGraph()
    g.add_edge('a', 'b')
    dot_g = exporters.nx_to_dot(g)
    ok_(isinstance(dot_g, pydot.Dot))