for i in range(gmm.n_components): plt.figure(i) alg = TotalGreedyCoverage(aoi2, out_trajectories2, 4, debug=False) mrs = alg.solution() assert mrs is not None, "solution not found" mrs.plot("TC-GaP") def __plot(self, title=None): plotter = ToursPlotManager(self.aoi, tours, labels=True, tour_labels=tour_labels) return plotter def plot(self, title=None): plotter = self.__plot(title) plotter.show() 经过for循环生成的多张图片plt.figure(i)上包含所有的轨迹曲线,不采用子图形式,修改如上程序,使得所有轨迹叠加在一张图片上,最终在图Figure上显示所有的轨迹曲线

时间: 2023-12-31 18:06:16 浏览: 26
您可以将所有轨迹曲线的绘制部分放在同一个子图中,然后在 for 循环结束后调用 plt.show() 来显示所有轨迹曲线的叠加图。以下是修改后的代码: ```python import matplotlib.pyplot as plt # 将所有轨迹绘制在同一个子图中 fig, ax = plt.subplots() for i in range(gmm.n_components): alg = TotalGreedyCoverage(aoi2, out_trajectories2, 4, debug=False) mrs = alg.solution() assert mrs is not None, "solution not found" # 将每个轨迹绘制在同一个子图中 mrs.plot("TC-GaP", ax) # 设置图像标题 plt.title("All Trajectories") # 显示图像 plt.show() ```
相关问题

for i in range(gmm.n_components): plt.figure(i) alg = TotalGreedyCoverage(aoi2, out_trajectories2, 4, debug=False) mrs = alg.solution() # get multi round solution assert mrs is not None, "solution not found" mrs.plot("TC-GaP") 经过for循环生成的多张图片plt.figure(i)上的曲线,如何能叠加在一张图片上

你可以在循环外先创建一个新的 figure 对象,然后在循环中使用 subplot 函数将每个子图添加到这个新的 figure 中,最终将所有的曲线叠加在同一张图片上。修改后的代码如下所示: ``` import matplotlib.pyplot as plt fig = plt.figure() # 创建新的 figure 对象 for i in range(gmm.n_components): plt.subplot(gmm.n_components, 1, i+1) # 添加子图 alg = TotalGreedyCoverage(aoi2, out_trajectories2, 4, debug=False) mrs = alg.solution() assert mrs is not None, "solution not found" mrs.plot("TC-GaP") plt.tight_layout() # 调整子图之间的间距 plt.show() # 显示图像 ``` 其中,`plt.subplot(gmm.n_components, 1, i+1)` 函数用于在当前 figure 对象中添加一个 `gmm.n_components` 行、1 列的子图,并将当前子图的编号设为 `i+1`。`plt.tight_layout()` 函数用于调整子图之间的间距,使得它们更加紧凑。最后,使用 `plt.show()` 函数显示图像。

for i in range(gmm.n_components): target = [(x, y) for x, y in random_target_points[a == i]] center_target = [(x, y) for x, y in [centers[i]]] aoi = AoI(center_target, target, width_area, height_area, 5) depots = aoi.depots trajectories_builder = DroneTrajGeneration(aoi) for drone in drones2: trajs2 = trajectories_builder.compute_trajectories(drone, depots[i]),报错 trajs2 = trajectories_builder.compute_trajectories(drone, depots[i]) IndexError: list index out of range,为什么

这个错误通常是因为您正在尝试使用一个超出列表范围的索引来访问列表中的元素。这可能是因为您的depots列表为空或者索引i超出了depots列表的范围。 建议您检查depots列表是否为空,并确保i的值小于depots列表的长度。如果depots列表为空,您可以尝试使用条件语句来避免出现这个错误。如果i的值超出了depots列表的范围,您可以尝试打印一下depots列表的长度和i的值来排查问题。

相关推荐

最新推荐

recommend-type

NR5G网络拒绝码-5gmm_cause = 111 (Protocol error, unspecified).docx

从3GPP协议和UE端行为分析5G gmm cause #111的网络问题
recommend-type

NR网络拒绝码-cause_value = 17 (0x11) (Network failure).docx

NR网络拒绝码-cause_value = 17 (0x11) (Network failure).docx
recommend-type

NR5G网络拒绝码-5gmm_cause = 7 (0x7) (5GS Service not allowed)

NR5G网络拒绝码-5gmm_cause = 7 (0x7) (5GS Service not allowed)
recommend-type

NR5G网络拒绝码-5gsm_cause = 36 (0x24) (Regular deactivation).docx

从3GPP协议和UE端行为分析5G gmm cause #36的网络问题
recommend-type

NR网络拒绝码-5gsm_cause = 29 (0x1d) (User authentication failed).docx

从3GPP协议和UE端行为分析5G gsm cause #29的网络问题
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。