之前在python中做旋翼机械臂的仿真时,需要查看姿态和位置数据,使用matplotlib作图,虽然很图不是很漂亮到那时都用python实现就很方便。结合操作.mat文件的博客:https://blog.csdn.net/qq_26879985/article/details/102605094,有如下代码,注释也很清晰。
# -*- coding: utf-8 -*-
"""
@author: Life696
仅供学习、交流使用
"""
# 程序功能,读取.mat文件,作出图像
import matplotlib.pyplot as plt
import numpy as np
import scipy.io as scio
step_time = 0.01 # 每一个数据之间的时间步长
matFile = "./AMS_data.mat"
mat_data = scio.loadmat(matFile)
datalen = len(mat_data["Euler_x_alpha"][0])
# 生成一个列表:起始0,中止int(step_time*datalen)-1,个数datalen,不包括最后一个数
time = np.linspace(0, int(step_time*datalen)-1 , datalen, endpoint = True)
plt.figure()
plt.subplot(3, 2, 1) # 3行2列,第1个
plt.plot(time, mat_data["Euler_x_alpha"][0], color='red', label='Euler_x_alpha')
plt.title("Euler_x_alpha")
plt.xlabel("time/s")
plt.ylabel("Angle[degree]")
plt.tight_layout()
plt.legend()
plt.subplot(3, 2, 3) # 3行2列,第3个(竖着第2个,横着数就是第3个)
plt.plot(time, mat_data["Euler_y_beta"][0], color='red', label='Euler_y_alpha')
plt.title("Euler_y_beta")
plt.xlabel("time/s")
plt.ylabel("Angle[degree]")
plt.tight_layout()
plt.legend()
plt.subplot(3, 2, 5)
plt.plot(time, mat_data["Euler_z_gamma"][0], color='red', label='Euler_z_alpha')
plt.title("Euler_z_gamma")
plt.xlabel("time/s")
plt.ylabel("Angle[degree]")
plt.tight_layout()
plt.legend()
plt.subplot(3, 2, 2)
plt.plot(time, mat_data["position_x"][0]*100, color='red', label='position_x')
plt.title("position_x")
plt.xlabel("time/s")
plt.ylabel("Position[m]")
plt.tight_layout()
plt.legend()
plt.subplot(3, 2, 4)
plt.plot(time, mat_data["position_y"][0]*100, color='red', label='position_y')
plt.title("position_y")
plt.xlabel("time/s")
plt.ylabel("Position[m]")
plt.tight_layout()
plt.legend()
plt.subplot(3, 2, 6)
plt.plot(time, mat_data["position_z"][0]*100, color='red', label='position_z')
plt.title("position_z")
plt.xlabel("time/s")
plt.ylabel("Position[m]")
plt.tight_layout()
plt.legend()
plt.show()
图像如下: