从列表中绘制 networkx 图
Draw networkx graph from lists
我有以下列表:
l1=["a", "a", "c", "d", "e", "f", "g", "h", "i"]
l2=["1", "2", "3", "4", "5", "6", "7", "8", "9"]
l3=["A", "B", "C", "D", "E", "F", "G", "H", "I"]
l4=[12, 14, 22, 33, 44, 55, 66, 77, 88]
l=[l1, l2, l3, l4]
并且我想从这些具有给定节点数的列表创建并绘制一个 networkx 图,以便在同一列表的元素之间有一条边。
上例中,假设节点数为20,则图节点数应为:
(('a'), ('1'), ('A'), (12), ('c'), ('3'), ('C'), (22), ('d'), ('4'), ('D'), (33))
没有考虑('a'), ('2'), ('B'), (14)
因为已经有一个节点'a'。因此,必须不考虑其他列表中相同位置的元素。
图边应为:
(['a', 'c'], ['c', 'd'], ['d', 'e'], ['e', 'f'],
['1', '3'], ['3', '4'], ['4', '5'], ['5', '6'],
['A', 'C'], ['C', 'D'], ['D', 'E'], ['E', 'F'],
[12, 22], [22, 33], [33, 44], [44, 55])
这些边必须是垂直的。我写了下面的代码:
import networkx as nx
G=nx.Graph()
for j in range(int(20/len(l))):
for i in range(len(l)):
if G.number_of_nodes()<20:
G.add_node(l[i][j], pos=(i*2, j))
if j>0 and l[i][j]!=l[i][j-1]:
G.add_edge(l[i][j], l[i][j-1])
但是好像不能正常使用。我得到的节点和边的列表是:
NodeView(('a', '1', 'A', 12, '2', 'B', 14, 'c', '3', 'C', 22, 'd', '4', 'D', 33, 'e', '5', 'E', 44))
EdgeView([('a', 'c'), ('1', '2'), ('A', 'B'), (12, 14), ('2', '3'), ('B', 'C'), (14, 22), ('c', 'd'), ('3', '4'), ('C', 'D'), (22, 33), ('d', 'e'), ('4', '5'), ('D', 'E'), (33, 44)])
边缘列表不像我上面描述的那样。任何帮助将不胜感激
import networkx as nx
def create_graph(l):
G=nx.Graph()
for j in range(len(l[0])):
if l[0][j] in G.nodes:
continue
for i in range(len(l)):
if G.number_of_nodes() >= 20:
return G
G.add_node(l[i][j], pos=(i*2, j))
if j>0 and l[i][j]!=l[i][j-1]:
G.add_edge(l[i][j], l[i][j-1])
l1=["a", "a", "c", "d", "e", "f", "g", "h", "i"]
l2=["1", "2", "3", "4", "5", "6", "7", "8", "9"]
l3=["A", "B", "C", "D", "E", "F", "G", "H", "I"]
l4=[12, 14, 22, 33, 44, 55, 66, 77, 88]
l=[l1, l2, l3, l4]
G = create_graph(l)
print(G.nodes)
print(G.edges)
我有以下列表:
l1=["a", "a", "c", "d", "e", "f", "g", "h", "i"]
l2=["1", "2", "3", "4", "5", "6", "7", "8", "9"]
l3=["A", "B", "C", "D", "E", "F", "G", "H", "I"]
l4=[12, 14, 22, 33, 44, 55, 66, 77, 88]
l=[l1, l2, l3, l4]
并且我想从这些具有给定节点数的列表创建并绘制一个 networkx 图,以便在同一列表的元素之间有一条边。 上例中,假设节点数为20,则图节点数应为:
(('a'), ('1'), ('A'), (12), ('c'), ('3'), ('C'), (22), ('d'), ('4'), ('D'), (33))
没有考虑('a'), ('2'), ('B'), (14)
因为已经有一个节点'a'。因此,必须不考虑其他列表中相同位置的元素。
图边应为:
(['a', 'c'], ['c', 'd'], ['d', 'e'], ['e', 'f'],
['1', '3'], ['3', '4'], ['4', '5'], ['5', '6'],
['A', 'C'], ['C', 'D'], ['D', 'E'], ['E', 'F'],
[12, 22], [22, 33], [33, 44], [44, 55])
这些边必须是垂直的。我写了下面的代码:
import networkx as nx
G=nx.Graph()
for j in range(int(20/len(l))):
for i in range(len(l)):
if G.number_of_nodes()<20:
G.add_node(l[i][j], pos=(i*2, j))
if j>0 and l[i][j]!=l[i][j-1]:
G.add_edge(l[i][j], l[i][j-1])
但是好像不能正常使用。我得到的节点和边的列表是:
NodeView(('a', '1', 'A', 12, '2', 'B', 14, 'c', '3', 'C', 22, 'd', '4', 'D', 33, 'e', '5', 'E', 44))
EdgeView([('a', 'c'), ('1', '2'), ('A', 'B'), (12, 14), ('2', '3'), ('B', 'C'), (14, 22), ('c', 'd'), ('3', '4'), ('C', 'D'), (22, 33), ('d', 'e'), ('4', '5'), ('D', 'E'), (33, 44)])
边缘列表不像我上面描述的那样。任何帮助将不胜感激
import networkx as nx
def create_graph(l):
G=nx.Graph()
for j in range(len(l[0])):
if l[0][j] in G.nodes:
continue
for i in range(len(l)):
if G.number_of_nodes() >= 20:
return G
G.add_node(l[i][j], pos=(i*2, j))
if j>0 and l[i][j]!=l[i][j-1]:
G.add_edge(l[i][j], l[i][j-1])
l1=["a", "a", "c", "d", "e", "f", "g", "h", "i"]
l2=["1", "2", "3", "4", "5", "6", "7", "8", "9"]
l3=["A", "B", "C", "D", "E", "F", "G", "H", "I"]
l4=[12, 14, 22, 33, 44, 55, 66, 77, 88]
l=[l1, l2, l3, l4]
G = create_graph(l)
print(G.nodes)
print(G.edges)