【100天精通Python】Day69:Python可视化_实战:导航定位中预测轨迹和实际轨迹的3D动画,示例+代码

简介: 【100天精通Python】Day69:Python可视化_实战:导航定位中预测轨迹和实际轨迹的3D动画,示例+代码

1. 预测的3D轨迹和实际轨迹的动画图,同时动态更新

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation, PillowWriter
# 假设您有两组连续平滑的姿势数据集,一组表示预测值,一组表示真值
# 每个数据点包含姿势信息 [x, y, z, roll, pitch, yaw]
# 这里使用一些示例数据,您需要替换为您的实际数据
num_poses = 200  # 增加轨迹点数
t = np.linspace(0, 20, num_poses)  # 时间点,使轨迹变得更长
# 生成示例数据来表示预测值轨迹
x_pred = np.sin(t)
y_pred = np.cos(t)
z_pred = np.linspace(0, 10, num_poses)
# 生成示例数据来表示真值轨迹
x_true = np.sin(t) + 0.5  # 真值轨迹稍微偏移
y_true = np.cos(t) + 0.5
z_true = np.linspace(0, 10, num_poses)
# 创建一个 3D 图形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 创建空的轨迹线,一个红色表示预测值,一个蓝色表示真值
line_pred, = ax.plot([], [], [], marker='o', linestyle='-', markersize=4, color='red', label='Predicted Trajectory')
line_true, = ax.plot([], [], [], marker='o', linestyle='-', markersize=4, color='green', label='True Trajectory')
# 设置图形标题和轴标签
ax.set_title('Pose Trajectories (Predicted vs. True)')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# 添加图例
ax.legend(loc='upper right')
# 初始化函数,用于绘制空轨迹线
def init():
    line_pred.set_data([], [])
    line_pred.set_3d_properties([])
    line_true.set_data([], [])
    line_true.set_3d_properties([])
    return line_pred, line_true
# 更新函数,用于更新轨迹线的数据
def update(frame):
    line_pred.set_data(x_pred[:frame], y_pred[:frame])
    line_pred.set_3d_properties(z_pred[:frame])
    line_true.set_data(x_true[:frame], y_true[:frame])
    line_true.set_3d_properties(z_true[:frame])
    # 扩大坐标范围,以包围轨迹
    ax.set_xlim(min(x_true) - 1, max(x_true) + 1)
    ax.set_ylim(min(y_true) - 1, max(y_true) + 1)
    ax.set_zlim(min(z_true) - 1, max(z_true) + 1)
    return line_pred, line_true
# 创建动画对象
ani = FuncAnimation(fig, update, frames=num_poses, init_func=init, blit=True)
# 创建一个文件名为animation.gif的视频文件,使用PillowWriter
ani.save('animation_gt.gif', writer=PillowWriter(fps=30))
# 显示动画
plt.show()

2 真值轨迹设置为静态的,预测轨迹不断更新

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation, PillowWriter
# 假设您有两组连续平滑的姿势数据集,一组表示预测值,一组表示真值
# 每个数据点包含姿势信息 [x, y, z, roll, pitch, yaw]
# 这里使用一些示例数据,您需要替换为您的实际数据
num_poses = 200  # 增加轨迹点数
t = np.linspace(0, 20, num_poses)  # 时间点,使轨迹变得更长
# 生成示例数据来表示预测值轨迹
x_pred = np.sin(t)
y_pred = np.cos(t)
z_pred = np.linspace(0, 10, num_poses)
# 生成示例数据来表示真值轨迹
x_true = np.sin(t) + np.random.uniform(-0.2, 0.3)  # 真值轨迹稍微偏移
y_true = np.sin(t) + np.random.uniform(-0.2, 0.3)  # 真值轨迹稍微偏移
z_true = np.linspace(0, 10, num_poses)
# 创建一个 3D 图形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 创建空的轨迹线,一个红色表示预测值,一个绿色表示真值
line_pred, = ax.plot([], [], [], marker='o', linestyle='-', markersize=4, color='red', label='Predicted Trajectory')
line_true, = ax.plot(x_true, y_true, z_true, marker='o', linestyle='-', markersize=4, color='green', label='True Trajectory')
# 设置图形标题和轴标签
ax.set_title('Pose Trajectories (Predicted vs. True)')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# 添加图例
ax.legend(loc='upper right')
# 设置轨迹显示范围
ax.set_xlim(-2, 2)  # X轴范围
ax.set_ylim(-2, 2)  # Y轴范围
ax.set_zlim(0, 12)  # Z轴范围
# 初始化函数,用于绘制空轨迹线
def init():
    line_pred.set_data([], [])
    line_pred.set_3d_properties([])
    return line_pred, line_true
# 更新函数,用于更新预测轨迹的数据
def update(frame):
    line_pred.set_data(x_pred[:frame], y_pred[:frame])
    line_pred.set_3d_properties(z_pred[:frame])
    return line_pred, line_true
# 创建动画对象
ani = FuncAnimation(fig, update, frames=num_poses, init_func=init, blit=True)
# 创建一个文件名为animation.gif的视频文件,使用PillowWriter
ani.save('animation_1.gif', writer=PillowWriter(fps=30))
# 显示动画
plt.show()

3 网格的三维坐标系有旋转运动,以此全方位展示预测轨迹和真值轨迹之间的空间关系

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation, PillowWriter
# 假设您有两组连续平滑的姿势数据集,一组表示预测值,一组表示真值
# 每个数据点包含姿势信息 [x, y, z, roll, pitch, yaw]
# 这里使用一些示例数据,您需要替换为您的实际数据
num_poses = 200  # 增加轨迹点数
t = np.linspace(0, 20, num_poses)  # 时间点,使轨迹变得更长
# 生成示例数据来表示预测值轨迹
x_pred = np.sin(t)
y_pred = np.cos(t)
z_pred = np.linspace(0, 10, num_poses)
# 生成示例数据来表示真值轨迹
x_true = np.sin(t) + 0.5  # 真值轨迹稍微偏移
y_true = np.cos(t) + 0.5
z_true = np.linspace(0, 10, num_poses)
# 创建一个 3D 图形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 创建空的轨迹线,一个红色表示预测值,一个蓝色表示真值
line_pred, = ax.plot([], [], [], marker='o', linestyle='-', markersize=4, color='red', label='Predicted Trajectory')
line_true, = ax.plot(x_true, y_true, z_true, marker='o', linestyle='-', markersize=4, color='blue', label='True Trajectory')
# 设置图形标题和轴标签
ax.set_title('Pose Trajectories (Predicted vs. True)')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# 添加图例
ax.legend(loc='upper right')
# 设置轨迹显示范围
ax.set_xlim(-2, 2)  # X轴范围
ax.set_ylim(-2, 2)  # Y轴范围
ax.set_zlim(0, 12)  # Z轴范围
# 初始化函数,用于绘制空轨迹线
def init():
    line_pred.set_data([], [])
    line_pred.set_3d_properties([])
    return line_pred, line_true
# 更新函数,用于更新预测轨迹的数据和整体的旋转运动
def update(frame):
    line_pred.set_data(x_pred[:frame], y_pred[:frame])
    line_pred.set_3d_properties(z_pred[:frame])
    # 添加整体的旋转运动
    ax.view_init(elev=20, azim=frame)  # 调整视角,azim控制旋转
    return line_pred, line_true
# 创建动画对象
ani = FuncAnimation(fig, update, frames=num_poses, init_func=init, blit=True)
# 创建一个文件名为animation.gif的视频文件,使用PillowWriter
ani.save('animation.gif', writer=PillowWriter(fps=30))
# 显示动画
plt.show()

更新函数中使用了ax.view_init来控制整体的旋转运动,elev参数用于调整仰角,azim参数用于控制旋转。您可以根据需要调整elevazim的值来实现所需的旋转效果。

image.png

目录
打赏
0
2
1
0
20
分享
相关文章
|
22天前
|
Python高性能编程:五种核心优化技术的原理与Python代码
Python在高性能应用场景中常因执行速度不及C、C++等编译型语言而受质疑,但通过合理利用标准库的优化特性,如`__slots__`机制、列表推导式、`@lru_cache`装饰器和生成器等,可以显著提升代码效率。本文详细介绍了这些实用的性能优化技术,帮助开发者在不牺牲代码质量的前提下提高程序性能。实验数据表明,这些优化方法能在内存使用和计算效率方面带来显著改进,适用于大规模数据处理、递归计算等场景。
58 5
Python高性能编程:五种核心优化技术的原理与Python代码
|
2月前
|
课程设计项目之基于Python实现围棋游戏代码
游戏进去默认为九路玩法,当然也可以选择十三路或是十九路玩法 使用pycharam打开项目,pip安装模块并引用,然后运行即可, 代码每行都有详细的注释,可以做课程设计或者毕业设计项目参考
78 33
【Azure Developer】Python代码调用Graph API将外部用户添加到组,结果无效,也无错误信息
根据Graph API文档,在单个请求中将多个成员添加到组时,Python代码示例中的`members@odata.bind`被错误写为`members@odata_bind`,导致用户未成功添加。
51 10
如何提高Python代码的性能:优化技巧与实践
本文探讨了如何提高Python代码的性能,重点介绍了一些优化技巧与实践方法。通过使用适当的数据结构、算法和编程范式,以及利用Python内置的性能优化工具,可以有效地提升Python程序的执行效率,从而提升整体应用性能。本文将针对不同场景和需求,分享一些实用的优化技巧,并通过示例代码和性能测试结果加以说明。
揭秘Python编程之美:从基础到进阶的代码实践之旅
【9月更文挑战第14天】本文将带领读者深入探索Python编程语言的魅力所在。通过简明扼要的示例,我们将揭示Python如何简化复杂问题,提升编程效率。无论你是初学者还是有一定经验的开发者,这篇文章都将为你打开一扇通往高效编码世界的大门。让我们开始这段充满智慧和乐趣的Python编程之旅吧!
探索机器学习:从理论到Python代码实践
【10月更文挑战第36天】本文将深入浅出地介绍机器学习的基本概念、主要算法及其在Python中的实现。我们将通过实际案例,展示如何使用scikit-learn库进行数据预处理、模型选择和参数调优。无论你是初学者还是有一定基础的开发者,都能从中获得启发和实践指导。
87 2
Python 高级编程:深入探索高级代码实践
本文深入探讨了Python的四大高级特性:装饰器、生成器、上下文管理器及并发与并行编程。通过装饰器,我们能够在不改动原函数的基础上增添功能;生成器允许按需生成值,优化处理大数据;上下文管理器确保资源被妥善管理和释放;多线程等技术则助力高效完成并发任务。本文通过具体代码实例详细解析这些特性的应用方法,帮助读者提升Python编程水平。
205 5
时间序列特征提取:从理论到Python代码实践
时间序列是一种特殊的存在。这意味着你对表格数据或图像进行的许多转换/操作/处理技术对于时间序列来说可能根本不起作用。
105 1
时间序列特征提取:从理论到Python代码实践
Python编程之魔法:从基础到进阶的代码实践
在编程的世界里,Python以其简洁和易读性而闻名。本文将通过一系列精选的代码示例,引导你从Python的基础语法出发,逐步探索更深层次的应用,包括数据处理、网络爬虫、自动化脚本以及机器学习模型的构建。每个例子都将是一次新的发现,带你领略Python编程的魅力。无论你是初学者还是希望提升技能的开发者,这些示例都将是你的宝贵财富。让我们开始这段Python编程之旅,一起揭开它的魔法面纱。
使用Python进行数据分析的新手指南深入浅出操作系统:从理论到代码实践
【8月更文挑战第30天】在数据驱动的世界中,掌握数据分析技能变得越来越重要。本文将引导你通过Python这门强大的编程语言来探索数据分析的世界。我们将从安装必要的软件包开始,逐步学习如何导入和清洗数据,以及如何使用Pandas库进行数据操作。文章最后会介绍如何使用Matplotlib和Seaborn库来绘制数据图表,帮助你以视觉方式理解数据。无论你是编程新手还是有经验的开发者,这篇文章都将为你打开数据分析的大门。

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等