使用分配的坐标绘制 networkx
Plot networkx with assigned coordinates
我正在尝试 运行 以下代码来绘制 networkx 图,但它给我类型错误:列表索引必须是整数或切片,而不是 str。你能帮我吗?
pos = [[6.886709112999999, 50.50618938]]
nodes = ['866','144']
edges = [['866', '144']]
G = nx.Graph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)
nx.draw_networkx(G, pos)
plt.show()
如果您想为 draw_networkx
指定位置,您应该提供一个位置字典 (x,y):
pos (dictionary, optional) – A dictionary with nodes as keys and positions as values. If not specified a spring layout positioning will be computed. See networkx.drawing.layout for functions that compute node positions.
您可以在此处 () 查看最近的示例。
或者用你的小例子:
import networkx as nx
import matplotlib.pylab as plt
pos = {'866':[6.886709112999999, 0], '144': [50.50618938, 0]}
nodes = ['866','144']
edges = [['866', '144']]
G = nx.Graph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)
nx.draw_networkx(G, pos)
plt.show()
您观察到的类型错误可能是由于网络试图调用您的 pos
列表并将节点作为索引,例如pos['866']
.
我正在尝试 运行 以下代码来绘制 networkx 图,但它给我类型错误:列表索引必须是整数或切片,而不是 str。你能帮我吗?
pos = [[6.886709112999999, 50.50618938]]
nodes = ['866','144']
edges = [['866', '144']]
G = nx.Graph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)
nx.draw_networkx(G, pos)
plt.show()
如果您想为 draw_networkx
指定位置,您应该提供一个位置字典 (x,y):
pos (dictionary, optional) – A dictionary with nodes as keys and positions as values. If not specified a spring layout positioning will be computed. See networkx.drawing.layout for functions that compute node positions.
您可以在此处 (
import networkx as nx
import matplotlib.pylab as plt
pos = {'866':[6.886709112999999, 0], '144': [50.50618938, 0]}
nodes = ['866','144']
edges = [['866', '144']]
G = nx.Graph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)
nx.draw_networkx(G, pos)
plt.show()
您观察到的类型错误可能是由于网络试图调用您的 pos
列表并将节点作为索引,例如pos['866']
.