【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

目录
相关文章
|
4天前
|
存储 Python
Python示例:分解一个不多于指定位的正整数
Python示例:分解一个不多于指定位的正整数
11 0
|
2天前
|
调度 开发者 UED
探索Python中的异步编程:从基础到实战
【9月更文挑战第30天】在编程的世界里,异步编程是一个强大的概念,它允许程序在等待某些操作完成时继续执行其他任务。本文将深入探讨Python中的异步编程,从理解其基本概念开始,逐步过渡到高级应用。我们将通过具体的代码示例来展示如何在实际项目中实现异步功能,从而提高应用程序的性能和响应性。无论你是初学者还是有经验的开发者,这篇文章都将为你提供宝贵的见解和实用技巧。
|
3天前
|
数据可视化 Python
Python数据可视化-动态柱状图可视化
Python数据可视化-动态柱状图可视化
|
4天前
|
Python
? Python 装饰器入门:让代码更灵活和可维护
? Python 装饰器入门:让代码更灵活和可维护
11 4
|
3天前
|
JSON 数据可视化 数据处理
Python数据可视化-折线图可视化
Python数据可视化-折线图可视化
|
4天前
|
缓存 测试技术 Python
探索Python中的装饰器:简化代码,提高可读性
【9月更文挑战第28天】在Python编程中,装饰器是一个强大的工具,它允许我们在不修改原有函数代码的情况下增加额外的功能。本文将深入探讨装饰器的概念、使用方法及其在实际项目中的应用,帮助读者理解并运用装饰器来优化和提升代码的效率与可读性。通过具体示例,我们将展示如何创建自定义装饰器以及如何利用它们简化日常的编程任务。
10 3
|
5天前
|
数据采集 人工智能 程序员
探索Python编程:从基础到实战
【9月更文挑战第27天】在这篇文章中,我们将一起踏上一段激动人心的Python编程之旅。无论你是初学者还是有一定经验的开发者,这里都有适合你的内容。文章将通过浅显易懂的语言带你了解Python的基础语法,并通过实际案例展示如何将这些知识应用于解决现实问题。准备好,我们即将启程!
|
3天前
|
机器学习/深度学习 数据格式 Python
将特征向量转化为Python代码
将特征向量转化为Python代码
|
4天前
|
Python
Python编程的循环结构小示例(二)
Python编程的循环结构小示例(二)
|
5天前
|
Python
Python 装饰器入门:让代码更灵活和可维护
Python 装饰器入门:让代码更灵活和可维护
10 1
下一篇
无影云桌面