Python绘制TSP、VRP问题求解结果图全过程

 更新时间:2025年09月25日 11:01:00   作者:Q.V.Q  
本文介绍用Python绘制TSP和VRP问题的静态与动态结果图,静态图展示路径,动态图通过matplotlib.animation模块实现动画效果,保存为GIF需安装PIL库支持

【代码】Python绘制TSP、VRP问题求解结果图(包含静态图与动态图)。

一、静态图

import matplotlib.pyplot as plt


def plot_tour(data, best_path, is_save):
    """
    绘制旅行图
    :param data: 包含位置坐标的字典类型数据
    :param best_path: 最优旅行路径
    :param is_save: 是否保存绘图
    :return:
    """
    x = []
    y = []
    text_list = []
    for v in best_path:
        x.append(data[v][0])
        y.append(data[v][1])
        text_list.append(str(v))

    for i in range(len(text_list)):
        plt.text(x[i], y[i], text_list[i], ha='center', va='center_baseline')

    plt.plot(x, y, 'co--', linewidth=2, markersize=12)
    if is_save:
        plt.savefig("best_tour.png")
    plt.show()


def vrp():
    data = {
        1: (710.0, 1310.0),
        2: (630.0, 1660.0),
        3: (40.0, 2090.0),
        4: (750.0, 1100.0),
        5: (750.0, 2030.0),
        6: (1030.0, 2070.0),
        7: (1650.0, 650.0),
        8: (1490.0, 1630.0),
        9: (790.0, 2260.0),
        10: (1150.0, 1760.0),
        11: (840.0, 550.0),
        12: (1170.0, 2300.0),
        13: (970.0, 1340.0),
        14: (510.0, 700.0),
        15: (750.0, 900.0),
        16: (1280.0, 1200.0),
        17: (230.0, 590.0),
        18: (460.0, 860.0),
        19: (1040.0, 950.0),
        20: (590.0, 1390.0),
        21: (830.0, 1770.0),
        22: (490.0, 500.0),
        23: (1840.0, 1240.0),
        24: (1260.0, 1500.0),
        25: (1280.0, 790.0),
        26: (490.0, 2130.0),
        27: (1460.0, 1420.0),
        28: (1260.0, 1910.0),
        29: (360.0, 1980.0)
    }
    best_path = [1, 4, 15, 18, 17, 14, 22, 11, 19, 25, 7, 23, 27, 8, 24, 16, 13, 1,
                 1, 21, 10, 28, 6, 12, 9, 5, 26, 29, 3, 2, 20, 1]
    plot_tour(data, best_path, False)


def tsp():
    data = {
        1: (1150.0, 1760.0),
        2: (630.0, 1660.0),
        3: (40.0, 2090.0),
        4: (750.0, 1100.0),
        5: (750.0, 2030.0),
        6: (1030.0, 2070.0),
        7: (1650.0, 650.0),
        8: (1490.0, 1630.0),
        9: (790.0, 2260.0),
        10: (710.0, 1310.0),
        11: (840.0, 550.0),
        12: (1170.0, 2300.0),
        13: (970.0, 1340.0),
        14: (510.0, 700.0),
        15: (750.0, 900.0),
        16: (1280.0, 1200.0),
        17: (230.0, 590.0),
        18: (460.0, 860.0),
        19: (1040.0, 950.0),
        20: (590.0, 1390.0),
        21: (830.0, 1770.0),
        22: (490.0, 500.0),
        23: (1840.0, 1240.0),
        24: (1260.0, 1500.0),
        25: (1280.0, 790.0),
        26: (490.0, 2130.0),
        27: (1460.0, 1420.0),
        28: (1260.0, 1910.0),
        29: (360.0, 1980.0)
    }
    best_path = [1, 28, 6, 12, 9, 5, 26, 29, 3, 2, 20, 10, 4, 15, 18, 17,
                 14, 22, 11, 19, 25, 7, 23, 27, 8, 24, 16, 13, 21, 1]
    plot_tour(data, best_path, False)


vrp()

TSP结果图

VRP结果图

二、动态图

原理:

利用matplotlib的animation模块进行动态图的制作,其中保存为gif图片时需要使用PIL包,否则无法保存。

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import PIL


def plot_tour(data, best_path, is_save):
    """
    绘制旅行图
    :param data: 包含位置坐标的字典类型数据
    :param best_path: 最优旅行路径
    :param is_save: 是否保存绘图
    :return:
    """
    fig, ax = plt.subplots()
    x = []
    y = []
    figure_list = []
    text_list = []
    for v in best_path:
        x.append(data[v][0])
        y.append(data[v][1])
        text_list.append(str(v))

        ax.plot(x, y, 'c^', linewidth=2, markersize=15)
        ax.text(data[v][0], data[v][1], str(v), ha='center', va='center_baseline', size=8)

        figure = ax.plot(x, y, '--', linewidth=2, markersize=20)

        figure_list.append(figure)
    ani = animation.ArtistAnimation(fig, figure_list, interval=200, repeat_delay=0)
    
    # 保存图片    
    ani.save("test.gif")

    plt.show()


def vrp():
    data = {
        1: (1150.0, 1760.0),
        2: (630.0, 1660.0),
        3: (40.0, 2090.0),
        4: (750.0, 1100.0),
        5: (750.0, 2030.0),
        6: (1030.0, 2070.0),
        7: (1650.0, 650.0),
        8: (1490.0, 1630.0),
        9: (790.0, 2260.0),
        10: (710.0, 1310.0),
        11: (840.0, 550.0),
        12: (1170.0, 2300.0),
        13: (970.0, 1340.0),
        14: (510.0, 700.0),
        15: (750.0, 900.0),
        16: (1280.0, 1200.0),
        17: (230.0, 590.0),
        18: (460.0, 860.0),
        19: (1040.0, 950.0),
        20: (590.0, 1390.0),
        21: (830.0, 1770.0),
        22: (490.0, 500.0),
        23: (1840.0, 1240.0),
        24: (1260.0, 1500.0),
        25: (1280.0, 790.0),
        26: (490.0, 2130.0),
        27: (1460.0, 1420.0),
        28: (1260.0, 1910.0),
        29: (360.0, 1980.0)
    }
    best_path = [10, 4, 15, 18, 17, 14, 22, 11, 19, 25, 7, 23, 27, 8, 24, 16, 13, 10,
                 10, 21, 1, 28, 6, 12, 9, 5, 26, 29, 3, 2, 20, 10]
    plot_tour(data, best_path, False)


def tsp():
    data = {
        1: (1150.0, 1760.0),
        2: (630.0, 1660.0),
        3: (40.0, 2090.0),
        4: (750.0, 1100.0),
        5: (750.0, 2030.0),
        6: (1030.0, 2070.0),
        7: (1650.0, 650.0),
        8: (1490.0, 1630.0),
        9: (790.0, 2260.0),
        10: (710.0, 1310.0),
        11: (840.0, 550.0),
        12: (1170.0, 2300.0),
        13: (970.0, 1340.0),
        14: (510.0, 700.0),
        15: (750.0, 900.0),
        16: (1280.0, 1200.0),
        17: (230.0, 590.0),
        18: (460.0, 860.0),
        19: (1040.0, 950.0),
        20: (590.0, 1390.0),
        21: (830.0, 1770.0),
        22: (490.0, 500.0),
        23: (1840.0, 1240.0),
        24: (1260.0, 1500.0),
        25: (1280.0, 790.0),
        26: (490.0, 2130.0),
        27: (1460.0, 1420.0),
        28: (1260.0, 1910.0),
        29: (360.0, 1980.0)
    }
    best_path = [1, 28, 6, 12, 9, 5, 26, 29, 3, 2, 20, 10, 4, 15, 18, 17,
                 14, 22, 11, 19, 25, 7, 23, 27, 8, 24, 16, 13, 21, 1]
    plot_tour(data, best_path, False)


tsp()

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • pandas to_excel 添加颜色操作

    pandas to_excel 添加颜色操作

    这篇文章主要介绍了pandas to_excel 添加颜色操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • Python 如何引用不确定的函数

    Python 如何引用不确定的函数

    在Python中,引用不确定的函数通常意味着我们可能在运行时才知道要调用哪个函数,或者我们可能想根据某些条件动态地选择不同的函数来执行,下面给大家分享Python 如何引用不确定的函数,感兴趣的朋友跟随小编一起看看吧
    2024-07-07
  • 使用Python matplotlib绘制简单的柱形图、折线图和直线图

    使用Python matplotlib绘制简单的柱形图、折线图和直线图

    Matplotlib是Python的绘图库, 它可与NumPy一起使用,提供了一种有效的MatLab开源替代方案,下面这篇文章主要给大家介绍了关于使用Python matplotlib绘制简单的柱形图、折线图和直线图的相关资料,需要的朋友可以参考下
    2022-08-08
  • python元组打包和解包过程详解

    python元组打包和解包过程详解

    在本篇文章里,我们给大家整理了关于python元组打包和解包过程的知识点内容,有兴趣点的朋友们可以跟着学习下。
    2021-08-08
  • 自适应线性神经网络Adaline的python实现详解

    自适应线性神经网络Adaline的python实现详解

    这篇文章主要介绍了自适应线性神经网络Adaline的python实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • pycharm内无法import已安装的模块问题解决

    pycharm内无法import已安装的模块问题解决

    今天小编就为大家分享一篇pycharm内无法import已安装的模块问题解决,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • Python中base64与xml取值结合问题

    Python中base64与xml取值结合问题

    这篇文章主要介绍了Python中base64与xml取值结合问题,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-12-12
  • python末尾逗号导致返回结果是一个元组的问题

    python末尾逗号导致返回结果是一个元组的问题

    在Python中,除非特别需要返回或传参元组,一般不推荐在语句末尾添加逗号,应该注意检查是否存在末尾逗号导致的这些副作用,这篇文章主要介绍了python末尾逗号导致返回结果是一个元组,需要的朋友可以参考下
    2023-09-09
  • Tensorflow 1.0之后模型文件、权重数值的读取方式

    Tensorflow 1.0之后模型文件、权重数值的读取方式

    今天小编就为大家分享一篇Tensorflow 1.0之后模型文件、权重数值的读取方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • Python中内置的日志模块logging用法详解

    Python中内置的日志模块logging用法详解

    Python的logging模块提供了记录程序运行情况的日志功能,类似于Apache的log4j,很好很强大,这里我们就来看一下Python中内置的日志模块logging用法详解
    2016-07-07

最新评论