如何解决使用 matplotlib python 绘制图形
我需要为以下 ACO TSP 解决方案绘制图形。我需要绘制的图表与此接近:
有我可以处理的代码片段吗?
解决方法
来自 tsplib95 的文档:
Converting problems
get_graph() creates a networkx.Graph instance from the problem data:
>>> G = problem.get_graph()
>>> G.nodes
NodeView((1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16))
所以,G 是一个 networkx
图,这意味着您可以使用 networkx
来绘制它:
import matplotlib.pyplot as plt
import networkx as nx
nx.draw_networkx(G)
plt.show()
编辑:绘制路径:
pos = nx.spring_layout(G)
H = nx.DiGraph(G) # convert to directed graph s.t. the edges have arrows.
nx.draw_networkx_nodes(H,pos=pos) # draw nodes
nx.draw_networkx_edges(H,pos=pos,alpha=0.1) # draw all edges with transparency
nx.draw_networkx_edges(H,edgelist=tour.path,edge_color='red') # highlight the edges in the path
plt.show()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。