【问题标题】:Networkx drawing layout for multilayer or group多层或组的 Networkx 绘图布局
【发布时间】:2021-03-03 12:40:12
【问题描述】:

我打算用python3和networkxmoduel画一个网络。

首先,很抱歉我无法编写任何示例代码,因为我没有收到任何原始数据。

网络由 3 组节点组成,下面附上的是我想象的。

这是手绘的。

我想参考任何布局或技巧来绘制这种上面。

我知道Multipartite Layouthttps://networkx.org/documentation/stable/auto_examples/drawing/plot_multipartite_graph.html#multipartite-layout,但是我不确定它是否适合我。

谢谢。

【问题讨论】:

    标签: python layout networkx


    【解决方案1】:

    多部分布局将根据您指定的分区将节点放在行/列中,但您似乎想要对齐节点,以便提供的组/分区聚集在一起并与其他组/集群。您可以通过制作一个可以传递给networkx 绘图函数的位置字典来做到这一点。下面的示例函数采用您的图形,图形对象中节点属性的名称,该名称指定每个节点属于哪个组/分区(partition_attr),一个可选的分区名称列表,指定您要显示的顺序您的组/组件从左到右 (partition_order) 和不同分区中节点之间的最小空间 (epsilon)。

    #%% Function to make position dicts by partition
    def make_node_positions(graph,partition_attr,partition_order=None,epsilon=.5):
        if not partition_order:
            # get a list of all the partition names if not specified
            partition_order = list(set(dict(graph.nodes(data=partition_attr)).values()))
            
        # make position dict for each partition
        orig_partition_pos_dicts = {partition:nx.spring_layout(graph.subgraph([node for node,part in graph.nodes(data=partition_attr)
                                                                                          if part == partition]))
                                    for partition in partition_order}
        
        # update the x coordinate in the position dicts so partitions
        # don't overlap and are in the specified order left-to-right
        final_pos_dict = orig_partition_pos_dicts[partition_order[0]]
        for i,partition in enumerate(partition_order[1:]):
            # get the largest x coordinate from the previous partition's nodes
            max_previous = max([x for x,y in final_pos_dict.values()])
            # get smallest x coordinate from this partition's nodes
            current_min = min([x for x,y in orig_partition_pos_dicts[partition].values()])
            # update the x coordinates for this partition to be at least epsilon units
            # to the right of the right-most node in the previous partition
            final_pos_dict.update({node:(pos[0]+max_previous+abs(current_min)+epsilon,pos[1])
                                                   for node,pos in orig_partition_pos_dicts[partition].items()})
        return(final_pos_dict)
    

    现在我已经制作了一个类似于你的绘图的图表并应用了下面的函数

    #%% Set up toy graph
    import networkx as nx
    
    # make the initial graphs
    k5 = nx.complete_graph(5)
    triangle=nx.from_edgelist([(5,6),(6,7),(5,7)])
    single_node = nx.Graph()
    single_node.add_node(8)
    
    # edges to connect the components
    extra_edges = [(3,5),(2,6),(5,8),(6,8),(7,8)]
    
    # combine graphs and specify the original graphs
    orig_graphs = {'k5':{'graph':k5,'color':'blue'},
                   'triangle':{'graph':triangle,'color':'green'},
                   'single_node':{'graph':single_node,'color':'red'}}
    g = nx.Graph()
    for g_name,g_val_dict in orig_graphs.items():
        # add the nodes from that graph and specify the partition and node colors
        g.add_nodes_from(g_val_dict['graph'].nodes,partition=g_name,color=g_val_dict['color'])
        if len(g_val_dict['graph'].edges) > 0:
            # if the graph has edges then add the edges
            g.add_edges_from(g_val_dict['graph'].edges,partition=g_name,color=g_val_dict['color'])
            
    # add the extra edges to combine the graphs
    g.add_edges_from(extra_edges,color='black')
    
    
    #%% Draw graph #####
    my_pos = make_node_positions(g,partition_attr='partition',partition_order=['k5','triangle','single_node'])
    nx.draw_networkx_nodes(g,my_pos,node_color=[c for n,c in g.nodes(data='color')])
    nx.draw_networkx_labels(g,my_pos)
    nx.draw_networkx_edges(g,my_pos,edge_color=[c for u,v,c in g.edges(data='color')])
    

    【讨论】:

      猜你喜欢
      • 2017-07-06
      • 1970-01-01
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多