之前在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()
圖像如下: