简体   繁体   English

使用 matplotlib python 绘制图形

[英]Plotting graph using matplotlib python

I need to plot the graph for the following solution of the ACO TSP.我需要 plot 以下 ACO TSP 解决方案的图表。 The graph that I need to plot would be something close to this:我需要 plot 的图表将接近于此:

ACO TSP 图

Any code snippets on which I could work on?我可以处理的任何代码片段吗?

From the documentation of tsplib95 :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))

So, G is a networkx graph, which means you can use networkx to draw it:所以,G 是一个networkx图,这意味着你可以使用networkx来绘制它:

import matplotlib.pyplot as plt
import networkx as nx

nx.draw_networkx(G)
plt.show()

edit: drawing the path:编辑:绘制路径:

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, pos=pos, edgelist=tour.path, edge_color='red') # highlight the edges in the path

plt.show()

I need to plot the graph for the following solution of the ACO TSP.我需要 plot 图表以获取 ACO TSP 的以下解决方案。 The graph that I need to plot would be something close to this:我需要 plot 的图表将与此接近:

ACO TSP 图

Any code snippets on which I could work on?我可以处理的任何代码片段?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM