使用matplotlib绘制动图

主程序如下:

python 复制代码
'''
项目:701项目三分报告
作者:WN
内容:速度障碍法实现
时间:2023年8月26号
'''
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import animation
from utils import *

uav1 = Agent(1, [0, 0])
trace1 = []
uav2 = Agent(2, [40, 40])
uav2.yaw = uav1.yaw + np.pi
trace2 = []

trace = []


for i in range(100):
    uav1.step()
    trace1.append((uav1.pos[0], uav1.pos[1]))
    uav2.step()
    trace2.append((uav2.pos[0], uav2.pos[1]))
    trace.append((uav1.pos[0], uav1.pos[1], uav2.pos[0], uav2.pos[1]))

# plt.xlim(0,40)
# plt.ylim(0,40)
# plt.scatter([x[0] for x in trace1], [x[1] for x in trace1], color='r')
# plt.scatter([x[0] for x in trace2], [x[1] for x in trace2], color='b')
# plt.show()


fig, ax = plt.subplots()
xdata1, ydata1 = [], []
xdata2, ydata2 = [], []

line1, = ax.plot(xdata1, ydata1, 'ro')
line2, = ax.plot(xdata2, ydata2, 'bo')

def init():
    ax.set_xlim(0,40)
    ax.set_ylim(0,40)
    return line1, line2
def update(frame):  # 帧
    print(frame)
    xdata1.append(frame[0])
    ydata1.append(frame[1])
    line1.set_data(xdata1, ydata1)

    xdata2.append(frame[2])
    ydata2.append(frame[3])
    line2.set_data(xdata2, ydata2)
    return line1,line2

ani = animation.FuncAnimation(
    fig=fig,
    func=update,
    frames=trace,
    init_func=init,
    interval=100, # 时间间隔
)
plt.show()
ani.save('1.gif', writer='pillow')

函数库如下:

python 复制代码
'''
作者:WN
内容:速度障碍法实现的主要函数
时间:2023年8月26号
'''
import numpy as np


class Agent(object):
    def __init__(self, ID, pos_0):
        self.id = ID
        self.v = 0.05  # 平台的速度(米/秒)
        self.pos = pos_0  # 平台的初始位置
        self.yaw = np.pi/3  # 平台的航向(0至2pi)
        self.det = 5  # 平台的探测范围
        self.state = 'None'  # 平台的状态(暂为缺省值)
        self.step_num = 0  # 记录平台仿真时间
        self.obs = None  # 记录平台所能观测到的数据

    def Stragey(self):
        '''
        根据智能体得到的观测数据来决策(输入、输出应根据需求具体设计)
        输入: 观测数据、当前位置、探测数据
        输出:新的速度
        '''
        pass

    def Pos_update(self):
        '''
        输入:当前位置、速度
        输出:更新后的位置
        '''
        self.pos[0] += self.v * np.cos(self.yaw)
        self.pos[1] += self.v * np.sin(self.yaw)
        pass

    def step(self):
        self.step_num += 1

        #########根据探测到的数据和策略解算新的位置、速度
        # self.v = self.Stragey()
        self.Pos_update()
相关推荐
小白学大数据5 天前
Pandas与Matplotlib:Python中的动态数据可视化
开发语言·爬虫·python·pandas·matplotlib
yu矞10 天前
时间序列预测学习方向总概括
python·学习·numpy·pandas·pillow·matplotlib
问道飞鱼10 天前
python常用库学习-Matplotlib使用
python·学习·matplotlib
知识在于积累11 天前
Python中matplotlib-legend图例水平排列
matplotlib·legend·图例水平排列
Trouvaille ~11 天前
【Python篇】matplotlib超详细教程-由入门到精通(上篇)
开发语言·python·数据分析·matplotlib·数据可视化·绘图·python3.11
西猫雷婶13 天前
python画图|3D图基础教程
开发语言·笔记·python·学习·3d·matplotlib
异构算力老群群15 天前
使用Python读取Excel数据
python·excel·numpy·pandas·matplotlib·ipython
flex_university15 天前
Matplotlib入门笔记
笔记·matplotlib
fydw_71518 天前
Matplotlib 详解
matplotlib
毛飞龙19 天前
Mac/Linux系统matplotlib中文支持问题
linux·mac·matplotlib·中文显示