1 回答
TA贡献1827条经验 获得超7个赞
我能够使用以下代码生成您似乎想要的图表 - 如果您遇到任何问题,请告诉我。您需要将zip对象转换为 a是正确的list,但我认为您的绘图代码中可能存在其他错误。nx.spring_layout如果您需要每次的输出都相同,则可以使用seed关键字参数,例如pos = nx.spring_layout(Gr, seed=123).
代码:
import networkx as nx
# Set up graph
Gr = nx.DiGraph()
edges = [(i+1, i+2) for i in range(10)] + [(i+2, i+1) for i in range(10)]
Gr.add_edges_from(edges)
# Get position using spring layout
pos = nx.spring_layout(Gr)
# Get shortest path
path = nx.shortest_path(Gr,source=1,target=7)
path_edges = list(zip(path,path[1:]))
# Draw nodes and edges not included in path
nx.draw_networkx_nodes(Gr, pos, nodelist=set(Gr.nodes)-set(path))
nx.draw_networkx_edges(Gr, pos, edgelist=set(Gr.edges)-set(path_edges), connectionstyle='arc3, rad = 0.3')
# Draw nodes and edges included in path
nx.draw_networkx_nodes(Gr, pos, nodelist=path, node_color='r')
nx.draw_networkx_edges(Gr,pos,edgelist=path_edges,edge_color='r', connectionstyle='arc3, rad = 0.3')
# Draw labels
nx.draw_networkx_labels(Gr,pos)
输出:
添加回答
举报