【问题标题】:Rotate k-partite graph in Network在网络中旋转 k 部图
【发布时间】:2020-04-11 12:47:41
【问题描述】:

我想将下面的 k 分图垂直旋转或旋转 45 度。我想以分层方式显示以下图,其中红色节点位于顶部,绿色节点集位于底部

网络文档只有 shell_layout 和边标签 networkx.drawing.nx_pylab.draw_networkx_edge_labels 的旋转选项

这是程序代码:

G = nx.Graph()
G.add_nodes_from(emc["entity"], bipartite=0)
G.add_nodes_from(set(EMM_unique["keys"]).symmetric_difference(set(emc["entity"])), bipartite=1)
G.add_nodes_from(EMM["id"], bipartite=2)
G.add_edges_from(list(emc.itertuples(index=False)))
G.add_edges_from(list(EMM.itertuples(index=False)))

nodes = G.nodes()
# for each of the parts create a set
nodes_0  = set([n for n in nodes if  G.nodes[n]['bipartite']==0])
nodes_1  = set([n for n in nodes if  G.nodes[n]['bipartite']==1])
nodes_2  = set([n for n in nodes if  G.nodes[n]['bipartite']==2])


 # set the location of the nodes for each set
pos = dict()
pos.update( (n, (1, i)) for i, n in enumerate(nodes_0) ) # put nodes from X at x=1
pos.update( (n, (2, i)) for i, n in enumerate(nodes_1) ) # put nodes from Y at x=2
pos.update( (n, (3, i)) for i, n in enumerate(nodes_2) ) # put nodes from X at x=1


color_map = []
for node in G:
    if node in emc["entity"].values:
       color_map.append("red")
    elif node in EMM["id"].values:
        color_map.append("green")
    else:
        color_map.append("blue")

nx.draw(G, pos, node_color=color_map, width= 2, with_labels=True, with_arrows=True)

这个solution只对翻转位置有用,对旋转没用。由于我没有逐个添加节点,因此solution 也不是很有帮助。

【问题讨论】:

    标签: python-3.x graph rotation networkx


    【解决方案1】:

    您是否考虑过使用 pygraphviz 或 hierarchy_pos 解决方案,而不是翻转图表,如下面的答案?

    Can one get hierarchical graphs from networkx with python 3?

    hierarchy_pos 解决方案在某种程度上对我来说效果很好:

     ## Converting the graph to an oriented tree through depth first search or breadth first search
    tree_g = nx.dfs_tree(g, <starting_node>)
    
    ## attempt to draw it like a tree
    pos = hierarchy_pos(tree_g) 
    nx.draw(tree_g, pos=pos,....)
    

    https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.algorithms.traversal.depth_first_search.dfs_tree.html

    【讨论】:

    • 我想以分层方式显示 k 部图。它实际上不是一棵树。这意味着它没有根。因此,当我应用上述解决方案时,它给出了一个正确的错误,即“TypeError: cannot use hierarchy_pos on a graph that is not a tree”
    • 我无法在我的电脑上安装 pygraphviz。
    【解决方案2】:

    我试图改变 position(pos) 变量的坐标,它起作用了。和我上面贴的有区别的部分代码有解决办法在这里

     # set the location of the nodes for each set
    pos = dict()
    pos.update( (n, (i, -1)) for i, n in enumerate(nodes_0) ) 
    pos.update( (n, (i, -2) for i, n in enumerate(nodes_1) ) 
    pos.update( (n, (i, -3)) for i, n in enumerate(nodes_2) ) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-31
      • 2010-12-28
      相关资源
      最近更新 更多