Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

networkx draw tripartite graph

import networkx as nx
import numpy as np

BG = nx.Graph()
source = ['s']
first = np.arange(3)
second = np.arange(3, 8)

BG.add_nodes_from(source, bipartite=0)
BG.add_nodes_from(first, bipartite=1)
BG.add_nodes_from(second, bipartite=2)
source_first_edges = []
first_second_edges = []

for f in first:
    source_first_edges.append(('s', f))
for s in second:
    for f in first:
        first_second_edges.append((f, s))

BG.add_edges_from(source_first_edges)
BG.add_edges_from(first_second_edges)

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

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

nx.draw_networkx(BG, pos=pos,)
Comment

PREVIOUS NEXT
Code Example
Python :: encanto meaning spanish 
Python :: how to access range of tuples in python 
Python :: python chunks iterator 
Python :: check processing bar of loop in python 
Python :: how to write list into csv file in python 
Python :: transverse tensor in pytorch 
Python :: Python - Cómo cruda la cuerda 
Python :: make a pop up window in python 
Python :: argmin returns one value for 2d array 
Python :: Display the number of observations inside a Seaborn boxplot 
Python :: Allow Complex Number like "1+2j" to be treated as valid number 
Python :: plot line2d on axis 
Python :: python check column conditions 
Python :: django how to delete a db field 
Python :: numpy.where() for substring 
Python :: how to make an infinite loop in python 
Python :: Get timestamp with pyrhon 
Python :: run exe for python and wait until finish 
Python :: import nifti to numpy 
Python :: xtick for axvline 
Python :: python if boolean example 
Python :: box detection 
Python :: online python compailer 
Python :: fromhex python 2.7 
Python :: nibabel expand dimension 
Python :: same quotes in a quotes 
Python :: pytest using tempfile 
Python :: least square fit straight line python 
Python :: gpg --verify Python-3.6.2.tgz.asc 
Python :: Creating sub elements in xml in python with ElementTree 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =