简体   繁体   中英

Bipartite graph problems

this one is my code...i dont know why the plot show like this

import networkx as nx
import matplotlib.pyplot as plt
import random
from networkx.algorithms import bipartite
B = nx.Graph()
B.add_nodes_from([1, 2, 3, 4], bipartite=0)
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_networkx(B)
plt.show(B)

why result look wrong?why dont show 2 set separately在此处输入图像描述

if i want to draw graph like this picture...what should i do?how to use bipartite layout, can anyone help me to write this code在此处输入图像描述

Try this:

import networkx as nx
import matplotlib.pyplot as plt
import random
from networkx.algorithms import bipartite
B = nx.Graph()
n = [1, 2, 3, 4]
l = [*'abc']
B.add_nodes_from(n, bipartite=0)
B.add_nodes_from(l, bipartite=1)
B.add_edges_from([(1, "a"), (1, "b"), (2, "b"), (2, "c"), (3, "c"), (4, "a")])
pos = dict()
pos.update( (n, (1, i)) for i, n in enumerate(n) ) 
pos.update( (n, (2, i)) for i, n in enumerate(l) ) 

nx.draw_networkx(B, pos=pos)
nx.draw_networkx_nodes(B, pos=pos, nodelist=n, node_color='G')
nx.draw_networkx_nodes(B,  node_shape='s', pos=pos, nodelist=l, node_color='r')

nx.draw_networkx_labels(B, pos, font_color='white',
                        labels={i:i for i in l})

plt.axis('off')
plt.show(B)

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM