Python读取.mat数据
并绘制1950~2019年的nino3.4时间序列
作者:第八星系-向显均
邮箱:13660328708@163.com
# 导入第三方库
import matplotlib.pyplot as plt
import numpy as np
import scipy.io as sio
# 导入.mat数据
SST = sio.loadmat("SST.mat")
nino34 = sio.loadmat("nino34.mat")
'''计算nino3.4指数'''
# 查看.mat数据
print(SST.keys())
print(nino34.keys())
# 读取数据
LON = SST['lon']
LAT = SST['lat']
sst = SST['SST']
ii = np.where((LON >= 190) & (LON <= 240))[0]
jj = np.where((LAT >= -5) & (LAT <= 5))[0]
i,j = np.meshgrid(ii,jj)
# 计算nino3.4指数
nino_index = np.mean(sst[i,j])
print(nino_index)
'''绘制1950~2019年的nino3.4时间序列'''
# 读取所需数据
NiNo34 = nino34['nino34']
# 数据处理
Y_NiNo = NiNo34.reshape(840,1)
Time_Year = np.linspace(1950,2020,840)
Average_NiNo_Index = np.mean(NiNo34)
El_nino = np.mean(NiNo34) + 0.5
La_nina = np.mean(NiNo34) - 0.5
# 绘制图像
h1 = plt.plot(Time_Year,Y_NiNo,color='blue',linewidth=1.5)
h2 = plt.axhline(y=El_nino,color='red',linestyle='--',linewidth=1.5)
h3=plt.axhline(y=Average_NiNo_Index,color='black',linestyle='--',linewidth=1.5)
h4 = plt.axhline(y=La_nina,color='green',linestyle='--',linewidth=1.5)
# 图像优化
plt.xlim([1950,2020])
plt.xlabel("Time(year)")
plt.ylabel("nio3.4 Index (℃)")
plt.title("nino3.4 Index from 1950 to 2019")
plt.legend(loc="upper left")
plt.legend([h1,h2,h3,h4],['nino3.4 index','El nino','Average','La nina'])
# 图像展示
plt.show()