当前位置:   article > 正文

Python 同时绘制多条三维动态轨迹曲线_python如何把飞机飞行轨迹叠加到有地图底图的三维图形中

python如何把飞机飞行轨迹叠加到有地图底图的三维图形中

Python 同时绘制多条三维动态轨迹

问题描述

最近做毕设,在用python做飞行器轨迹的数值仿真,在研究怎么样才能把多架飞机的三维坐标构成的数组画在同一个动图里。这个问题两三天才解决,因为关注三维曲线动图绘制的人比较少,所以在此作个总结,希望我能成为你们继续向上探索的肩膀。

方法介绍

我找到的方法有两种:

方法一:利用FuncAnimation函数:

  1. 优点 :生成的动图为曲线,而非一个一个的点;
  2. 缺点 :不能在生成位置坐标的同时生成一个显示一个;

方法二:利用plt.ion()打开交互模式:

  1. 优点 :能够实时与画布交互,添加新的点;
  2. 缺点 :只能画散点图,就算是一条线的轨迹也是由点构成的,影响美观;

由于展示需要,我选择了方法一。函数的具体用法在matplotlib官网1上有详细的教程,可以了解各个参数的意义。

代码展示

由于我的代码还没写完,所以我选择了官网上的例子2来展示运行效果:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

# Fixing random state for reproducibility
np.random.seed(19680801)


def random_walk(num_steps, max_step=0.05):
    """Return a 3D random walk as (num_steps, 3) array."""
    start_pos = np.random.random(3)
    steps = np.random.uniform(-max_step, max_step, size=(num_steps, 3))
    walk = start_pos + np.cumsum(steps, axis=0)
    return walk


def update_lines(num, walks, lines):
    for line, walk in zip(lines, walks):
        # NOTE: there is no .set_data() for 3 dim data...
        line.set_data(walk[:num, :2].T)
        line.set_3d_properties(walk[:num, 2])
    return lines


# Data: 40 random walks as (num_steps, 3) arrays
num_steps = 30
walks = [random_walk(num_steps) for index in range(40)]
# Attaching 3D axis to the figure
fig = plt.figure()
ax = fig.add_subplot(projection="3d")

# Create lines initially without data
lines = [ax.plot([], [], [])[0] for _ in walks]

# Setting the axes properties
ax.set(xlim3d=(0, 1), xlabel='X')
ax.set(ylim3d=(0, 1), ylabel='Y')
ax.set(zlim3d=(0, 1), zlabel='Z')

# Creating the Animation object
ani = animation.FuncAnimation(
    fig, update_lines, num_steps, fargs=(walks, lines), interval=100)

plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

效果展示

我知道,你可能和我一样懒,根本不会打开官网去看展示效果,所以我贴心的把运行结果贴在这里~

运行效果展示
唯一美中不足的是,我尚未找到哪里能主动设置线条颜色,以及加图例之类的。

资料来源

主要参考matplotlib2官网,同时从大佬的文章3中了解到了两种不同的方法,以及在b站视频4中详细了解到了FuncAnimation函数每一个参数的含义。

通过这次的经历,个人感觉官网是最好用的,如果有网站能比官网好用,那就只可能是——中文官网~


  1. 官网函数说明 ↩︎

  2. 官网示例 ↩︎ ↩︎

  3. 另一篇文章链接 ↩︎

  4. b站视频链接 ↩︎

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/543709
推荐阅读
相关标签
  

闽ICP备14008679号