在网络中旋转 k 分图

Rotate k-partite graph in Network

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

网络文档只有 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)

这个 is only good to flip the position and is not useful for rotation. As I am not adding the nodes one by one therefore, this solution也不是很有用。

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

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

我尝试更改 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) )