How to display two-way graphs with python networkX package?

How to display a bipartite graph in a python networkX package with nodes from one class in the column on the left and the rest with another class on the right?

I can create a graph and display it as follows

B = nx.Graph()
B.add_nodes_from([1,2,3,4], bipartite=0) # Add the node attribute "bipartite"
B.add_nodes_from(['a','b','c'], bipartite=1)
B.add_edges_from([(1,'a'), (1,'b'), (2,'b'), (2,'c'), (3,'c'), (4,'a')])
nx.draw(B)
plt.show()

But I want the nodes 1,2,3,4 on the left in the column and the nodes 'a', 'b', 'c' in the column on the right, with edges in between.

+4
source share
2 answers

You need to set the positions for each node yourself:

B = nx.Graph()
B.add_nodes_from([1,2,3,4], bipartite=0) # Add the node attribute "bipartite"
B.add_nodes_from(['a','b','c'], bipartite=1)
B.add_edges_from([(1,'a'), (1,'b'), (2,'b'), (2,'c'), (3,'c'), (4,'a')])

# Separate by group
l, r = nx.bipartite.sets(B)
pos = {}

# Update position for node from each group
pos.update((node, (1, index)) for index, node in enumerate(l))
pos.update((node, (2, index)) for index, node in enumerate(r))

nx.draw(B, pos=pos)
plt.show()

enter image description here

+2
source

, @Rikka - , .

def position_MultiPartiteGraph( Graph, Parts ):
  # Graph is a networkX Graph object, where the nodes have attribute 'agentType' with part name as a value
  # Parts is a list of names for the parts (to be shown as columns)
  # returns list of dictionaries with keys being networkX Nodes, values being x,y coordinates for plottingxPos = {}
  xPos = {}
  yPos = {}
  for index1, agentType in enumerate(Parts):
     xPos[agentType] = index1
     yPos[agentType] = 0

  pos = {}
  for node, attrDict in Graph.nodes(data=True):
     agentType = attrDict['agentType']
     # print ('node: %s\tagentType: %s' % (node, agentType))
     # print ('\t(x,y): (%d,%d)' % (xPos[agentType], yPos[agentType]))
     pos[node] = (xPos[agentType], yPos[agentType])
     yPos[agentType] += 1

  return pos

, , ( ):

TG = nx.Graph()
TG.add_nodes_from([1,2,3,4], agentType='world') # Add the node attribute   "bipartite"
TG.add_nodes_from(['a','b','c'], agentType='sender')
TG.add_nodes_from(['A','B','C'], agentType='receiver')

# This is just an easier way to add (and to automatically generate) weighted edges
myEdges = [(1,'a',0.75),
       (1,'b',0.25),
       (2,'b',0.5),
       (2,'c',0.5),
       (3,'c',1.0),
       (4,'a',1.0),
       ('a','C',0.10),
       ('a','A',0.80),
       ('c','A',1.0),
       ('b','C',1.0)]

[TG.add_edge(x,y,weight=z) for x,y, z in myEdges]

:

nx.draw(TG,pos=position_MultiPartiteGraph(TG, ['world', 'sender', 'receiver']))
plt.show()

, , ! ! @Rikka!

0

Source: https://habr.com/ru/post/1629293/


All Articles