【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

目录
相关文章
|
1月前
|
测试技术 Python
Python装饰器:为你的代码施展“魔法”
Python装饰器:为你的代码施展“魔法”
228 100
|
1月前
|
开发者 Python
Python列表推导式:一行代码的艺术与力量
Python列表推导式:一行代码的艺术与力量
319 95
|
1月前
|
缓存 Python
Python装饰器:为你的代码施展“魔法
Python装饰器:为你的代码施展“魔法
148 88
|
2月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
258 102
|
2月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
292 104
|
2月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
247 103
|
2月前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
184 82
|
25天前
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
153 3
|
25天前
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
333 3
|
1月前
|
并行计算 安全 计算机视觉
Python多进程编程:用multiprocessing突破GIL限制
Python中GIL限制多线程性能,尤其在CPU密集型任务中。`multiprocessing`模块通过创建独立进程,绕过GIL,实现真正的并行计算。它支持进程池、队列、管道、共享内存和同步机制,适用于科学计算、图像处理等场景。相比多线程,多进程更适合利用多核优势,虽有较高内存开销,但能显著提升性能。合理使用进程池与通信机制,可最大化效率。
235 3

推荐镜像

更多