【问题标题】:Order of nodes with networkx.drawing.layout.circular_layoutnetworkx.drawing.layout.circular_layout 的节点顺序
【发布时间】:2020-06-14 12:53:09
【问题描述】:

我对python编码比较陌生,我正在使用以下代码生成一个图表,有40个节点,节点的布局使用此代码给出:

 pos = nx.circular_layout(graph_anchor, **kwargs)

以下代码创建graph_anchor:

    graph_anchor = convert_ebunch_to_graph(ebunch_anchor)

def convert_ebunch_to_graph(ebunch):
    g = nx.DiGraph()
    g.add_weighted_edges_from(ebunch)

我的问题是,我如何计算出节点的绘制顺序,即我如何计算出这段代码如何决定将哪些节点放在一起以及以什么顺序排列它们?

【问题讨论】:

    标签: python networking graph nomachine-nx


    【解决方案1】:

    来自nx.circular_layout的来源:

    def circular_layout(G, dim=2, scale=1, center=None):
        # dim=2 only
        """Position nodes on a circle.
    
        Parameters
        ----------
        G : NetworkX graph or list of nodes
    
        dim : int
           Dimension of layout, currently only dim=2 is supported
    
        scale : float
            Scale factor for positions
    
        center : array-like or None
           Coordinate pair around which to center the layout.
    
        Returns
        -------
        dict :
           A dictionary of positions keyed by node
    
        Examples
        --------
        >>> G=nx.path_graph(4)
        >>> pos=nx.circular_layout(G)
    
        Notes
        ------
        This algorithm currently only works in two dimensions and does not
        try to minimize edge crossings.
    
        """
        import numpy as np
    
        G, center = process_params(G, center, dim)
    
        if len(G) == 0:
            pos = {}
        elif len(G) == 1:
            pos = {G.nodes()[0]: center}
        else:
            # Discard the extra angle since it matches 0 radians.
            theta = np.linspace(0, 1, len(G) + 1)[:-1] * 2 * np.pi
            theta = theta.astype(np.float32)
            pos = np.column_stack([np.cos(theta), np.sin(theta)])
            pos = _rescale_layout(pos, scale=scale) + center
            pos = dict(zip(G, pos))
    
        return pos
    

    似乎位置是通过将 360 度除以节点数来生成的。 哪个节点最终在哪里由这一行决定:

            pos = dict(zip(G, pos))
    

    zip(G, pos) 按顺序遍历图的节点。并为他们分配职位。如果你想改变位置,你需要改变顺序。

    例子:

    # make dummy graph
    G = nx.from_numpy_array(np.random.rand(12,12)>0.5)
    
    # test order of nodes:
    for node in G.nodes:
        print(node)
    
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    pos = nx.circular_layout(G)
    nx.draw_networkx(G, pos=pos)
    

    这里,分配的第一个位置是节点0的位置,然后我们逆时针走。

    我找不到一种简单的方法来更改 G 中节点的顺序,所以这里有一个小变通方法,它可以创建一个具有随机顺序的新图:

    nodes = list(G.nodes(data=True))
    edges = list(G.edges(data=True))
    np.random.shuffle(nodes)
    
    H=nx.Graph()
    H.add_nodes_from(nodes)
    H.add_edges_from(edges)
    
    pos = nx.circular_layout(H)
    nx.draw_networkx(H, pos=pos)
    

    因此,更改图中节点的顺序会改变它们在圆形布局中的最终位置。

    【讨论】:

    • 那么包含节点的字典决定了结构?
    • 字典没有排序,所以我不这么认为。节点存储在 G.nodes() 中,它返回一个 nodeview 对象。在任何情况下,如果您从列表中添加节点,该列表的顺序似乎会被保留。
    猜你喜欢
    • 1970-01-01
    • 2019-11-22
    • 2012-06-11
    • 1970-01-01
    • 2016-06-20
    • 2019-01-27
    • 2019-03-22
    • 1970-01-01
    • 2013-07-03
    相关资源
    最近更新 更多