0

I would like to rotate the following k-partite graph vertically or at 45 degrees. I want to show the following plot in a hierarchical way where red nodes are at the top and green node-set are at the bottomthis

Documentation of networks only have a rotate option for shell_layout and for edges labels networkx.drawing.nx_pylab.draw_networkx_edge_labels

Here is the program code:

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)

This solution 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 is not very helpful as well.

user3050590
  • 1,656
  • 4
  • 21
  • 40

2 Answers2

0

Instead of flipping the graph, have you looked into using pygraphviz or the hierarchical_pos solution as in the answer below?

Can one get hierarchical graphs from networkx with python 3?

the hierarchy_pos solution worked well to some extent for me:

 ## 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

emiljoj
  • 399
  • 1
  • 7
  • I want to show a k-partite graph in a hierarchical way. It actually is not a tree. That means it has no root. Thus it gives a right error when I applied the above solution i.e. "TypeError: cannot use hierarchy_pos on a graph that is not a tree" – user3050590 Apr 12 '20 at 07:30
  • I am not able to install pygraphviz on my computer. – user3050590 Apr 12 '20 at 07:33
0

I tried to change the coordinates at the position(pos) variable and it worked. The part of the code that is different from what I posted above and has a solution is here

 # 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) ) 
user3050590
  • 1,656
  • 4
  • 21
  • 40