在 networkx 中制作二分图
Make a bipartite graph in networkx
我想使用 networkx 制作二分图。我正在关注 documentation and
df = pd.DataFrame({'Name': ['John','John','Aron','Aron','Jeny','Jeny'],
'Movie':['A','B','C','A','Y','Z']})
G = nx.Graph()
G.add_nodes_from(df.Name, bipartite=0)
G.add_nodes_from(df.Movie, bipartite=1)
G.add_edges_from(df.values)
因为我的图表断开连接,即
nx.is_connected(G)
>False
top = nx.bipartite.sets(G)[0]
>AmbiguousSolution
我遵循文档如下:
top_nodes = {n for n, d in G.nodes(data=True) if d["bipartite"] == 0}
Z = nx.bipartite.projected_graph(G, top_nodes)
nx.draw(Z)
我得到:
我预计:
我无法重现您的问题。我复制了你的代码并得到了正确的图表。
>>> import networkx as nx
>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> G = nx.Graph()
>>> G.add_nodes_from(df.Name, bipartite=0)
>>> G.nodes
NodeView(('John', 'Aron', 'Jeny'))
>>> G.add_nodes_from(df.Movie, bipartite=1)
>>> G.nodes
NodeView(('John', 'Aron', 'Jeny', 'A', 'B', 'C', 'Y', 'Z'))
>>> G.add_edges_from(df.values)
>>> G.edges
EdgeView([('John', 'A'), ('John', 'B'), ('Aron', 'C'),
('Aron', 'A'), ('Jeny', 'Y'), ('Jeny', 'Z')])
>>> nx.draw(G, with_labels=True)
>>> plt.show()
您可以强制节点的位置遵循图的二分性质,遵循 this answer:
>>> people={n for n,d in G.nodes(data=True) if d['bipartite']==0}
>>> movies=set(G) - people
>>> pos = {n: (1,i) for i,n in enumerate(people)}
>>> pos.update({n: (2,i) for i,n in enumerate(movies)})
>>> nx.draw(G, with_labels=True, pos=pos)
>>> plt.show()
或this answer:
>>> people={n for n,d in G.nodes(data=True) if d['bipartite']==0}
>>> nx.draw(G, pos=nx.bipartite_layout(G, people), with_labels=True)
>>> plt.show()
使用:
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
df = pd.DataFrame(
{
"Name": ["John", "John", "Aron", "Aron", "Jeny", "Jeny"],
"Movie": ["A", "B", "C", "A", "Y", "Z"],
}
)
G = nx.Graph()
G.add_nodes_from(df.Name, bipartite=0)
G.add_nodes_from(df.Movie, bipartite=1)
G.add_edges_from(df.values)
pos = nx.bipartite_layout(G, df.Name)
nx.draw(G, pos=pos, with_labels=True)
我得到:
Please be aware that each time you generate a graph it will sort nodes randomly
我想使用 networkx 制作二分图。我正在关注 documentation and
df = pd.DataFrame({'Name': ['John','John','Aron','Aron','Jeny','Jeny'],
'Movie':['A','B','C','A','Y','Z']})
G = nx.Graph()
G.add_nodes_from(df.Name, bipartite=0)
G.add_nodes_from(df.Movie, bipartite=1)
G.add_edges_from(df.values)
因为我的图表断开连接,即
nx.is_connected(G)
>False
top = nx.bipartite.sets(G)[0]
>AmbiguousSolution
我遵循文档如下:
top_nodes = {n for n, d in G.nodes(data=True) if d["bipartite"] == 0}
Z = nx.bipartite.projected_graph(G, top_nodes)
nx.draw(Z)
我得到:
我预计:
我无法重现您的问题。我复制了你的代码并得到了正确的图表。
>>> import networkx as nx
>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> G = nx.Graph()
>>> G.add_nodes_from(df.Name, bipartite=0)
>>> G.nodes
NodeView(('John', 'Aron', 'Jeny'))
>>> G.add_nodes_from(df.Movie, bipartite=1)
>>> G.nodes
NodeView(('John', 'Aron', 'Jeny', 'A', 'B', 'C', 'Y', 'Z'))
>>> G.add_edges_from(df.values)
>>> G.edges
EdgeView([('John', 'A'), ('John', 'B'), ('Aron', 'C'),
('Aron', 'A'), ('Jeny', 'Y'), ('Jeny', 'Z')])
>>> nx.draw(G, with_labels=True)
>>> plt.show()
您可以强制节点的位置遵循图的二分性质,遵循 this answer:
>>> people={n for n,d in G.nodes(data=True) if d['bipartite']==0}
>>> movies=set(G) - people
>>> pos = {n: (1,i) for i,n in enumerate(people)}
>>> pos.update({n: (2,i) for i,n in enumerate(movies)})
>>> nx.draw(G, with_labels=True, pos=pos)
>>> plt.show()
或this answer:
>>> people={n for n,d in G.nodes(data=True) if d['bipartite']==0}
>>> nx.draw(G, pos=nx.bipartite_layout(G, people), with_labels=True)
>>> plt.show()
使用:
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
df = pd.DataFrame(
{
"Name": ["John", "John", "Aron", "Aron", "Jeny", "Jeny"],
"Movie": ["A", "B", "C", "A", "Y", "Z"],
}
)
G = nx.Graph()
G.add_nodes_from(df.Name, bipartite=0)
G.add_nodes_from(df.Movie, bipartite=1)
G.add_edges_from(df.values)
pos = nx.bipartite_layout(G, df.Name)
nx.draw(G, pos=pos, with_labels=True)
我得到:
Please be aware that each time you generate a graph it will sort nodes randomly