无法将边添加到 networkx 图
Can't add edges to a networkx graph
所以我以前从未使用过 Networkx,所以这可能是基本的。我目前正在使用几个函数来输出简单图形的字典,例如循环图。例如,如果我 运行 n = 5 的函数,我得到字典:{1: [5, 2], 2: [1, 3], 3: [2, 4], 4: [3 , 5], 5: [4, 1]}.
我已经开始尝试使用 Networkx 来可视化这些并使用以下函数来输出图形的边缘:
def generate_edges(graph): # generate edges of the graph
edges = []
for node in graph: # checks each node
for neighbour in graph[node]:
edges.append((node,neighbour)) # returns the node,neighbour pair as an edge
return edges
然后我使用以下函数来创建网络可视化:
def visualise(graph):
edges = generate_edges(graph)
nodes = list(graph.keys())
G = nx.graph
G.add_edges_from(edges)
G.nodes(data=True)
nx.write_graphml(G,'so.graphml')
print(nx.info(G))
return G
graph = generate_simplegraph('cycle',10)
visualise(graph)
其中 'cycle' 是图的类型,10 是节点数。我不断收到错误消息:
模块'networkx.classes.graph'没有属性'add_edges_from'
谁能告诉我为什么会出现此错误以及如何解决?我想要做的就是可视化网络。此外,我已将 networkx 导入为 nx。
我相信 graph 应该是 networkx 中的 class。
G=nx.Graph()
不是:
G = nx.graph
所以我以前从未使用过 Networkx,所以这可能是基本的。我目前正在使用几个函数来输出简单图形的字典,例如循环图。例如,如果我 运行 n = 5 的函数,我得到字典:{1: [5, 2], 2: [1, 3], 3: [2, 4], 4: [3 , 5], 5: [4, 1]}.
我已经开始尝试使用 Networkx 来可视化这些并使用以下函数来输出图形的边缘:
def generate_edges(graph): # generate edges of the graph
edges = []
for node in graph: # checks each node
for neighbour in graph[node]:
edges.append((node,neighbour)) # returns the node,neighbour pair as an edge
return edges
然后我使用以下函数来创建网络可视化:
def visualise(graph):
edges = generate_edges(graph)
nodes = list(graph.keys())
G = nx.graph
G.add_edges_from(edges)
G.nodes(data=True)
nx.write_graphml(G,'so.graphml')
print(nx.info(G))
return G
graph = generate_simplegraph('cycle',10)
visualise(graph)
其中 'cycle' 是图的类型,10 是节点数。我不断收到错误消息:
模块'networkx.classes.graph'没有属性'add_edges_from'
谁能告诉我为什么会出现此错误以及如何解决?我想要做的就是可视化网络。此外,我已将 networkx 导入为 nx。
我相信 graph 应该是 networkx 中的 class。
G=nx.Graph()
不是:
G = nx.graph