Arctic Oscillation 气象Python分析作业

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

ao = pd.read_csv(r”E:\python\44\AO.txt”, sep=’\s+’, header=None, names=[‘year’,’month’,’AO’])

ao_nov = ao[ao.month==11]
#print(ao)

fig = plt.figure(figsize=(12,8), dpi=200)

ax1 = fig.add_subplot(1,1,1)
ax1.plot(np.arange(1950,2020,1),ao_nov.AO, ‘ko-‘,label=’Nov’, color=’navy’)

ax1.set_title(‘1950-2019 Jan AO index’, font={‘family’: ‘Calibri’, ‘size’:28})

ax1.axhline(0,ls=’:’,c=’r’)

ax1.axvline(1990,ls=’–‘,c=’g’)

plt.tick_params(labelsize=20)

font1 = {‘family’: ‘Times New Roman’, ‘weight’:’normal’, ‘size’:28}
ax1.legend(prop=font1)
plt.title(‘Arctic Oscillation Index for November’, fontsize=20)
plt.show()

分析1950-2019的AO数据,取11月数据画图

import pandas as pd
from mpl_toolkits.axisartist.parasite_axes import HostAxes, ParasiteAxes
import numpy as np
import matplotlib.pyplot as plt

ao = pd.read_csv(r”E:\python\44\AO.txt”,
sep=’\s+’,header=None, names=[ ‘year’, ‘month’, ‘AO’])

plt.rcParams.update({‘font.family’:’monospace’})

ao_dec = ao[ao.month==12]
ao_jan = ao[ao.month==1]
ao_feb = ao[ao.month==2]
ao_djf = (ao_jan.AO.values+ao_feb.AO.values+ao_dec.AO.values)/3

fig = plt.figure(figsize=(13,8),dpi=200)
ax = HostAxes(fig, [0, 0, 0.9, 0.9])
ax1 = ParasiteAxes(ax, sharex=ax)
ax2 = ParasiteAxes(ax, sharex=ax)
ax3 = ParasiteAxes(ax, sharex=ax)
ax.parasites.append(ax1)
ax.parasites.append(ax2)
ax.parasites.append(ax3)
ax.axis[‘right’].set_visible(False)
ax1.axis[‘right’].set_visible(True)
ax1.axis[ ‘right’].major_ticklabels.set_visible(True)
ax1.axis[ ‘right’].label.set_visible(True)

ax.set_ylabel(‘DJF’)
ax.set_xlabel( ‘year’)
ax1.set_ylabel( ‘Dec’)
ax2.set_ylabel( ‘Jan’)
ax3.set_ylabel(‘Feb’)
axisline2 = ax2.get_grid_helper().new_fixed_axis
axisline3 = ax3.get_grid_helper().new_fixed_axis
ax2.axis[‘right2′] = axisline2(loc=’right’, axes=ax2, offset=(40,0))
ax3.axis[‘right3′] = axisline3(loc=’right’, axes=ax3, offset=(80,0))

fig.add_axes(ax)

ax.plot(np.arange(1950,2020,1),ao_djf, label=”DJF”, color=’black’)
ax1.plot(np.arange(1950,2020,1), ao_dec.AO, label=”Dec”, color=’peru’)
ax2.plot(np.arange(1950,2020,1), ao_jan.AO, label=”Jan”, color=’lime’)
ax3.plot(np.arange(1950,2020,1), ao_feb.AO, label=”Feb”, color=’violet’)
ax1.set_ylim(-4,4)
ax2.set_ylim(-5,5)
ax3.set_ylim(-6,6)
ax.legend()

ax1.axis[‘right’].major_ticks.set_color(‘peru’)
ax2.axis[‘right2’].major_ticks.set_color(‘lime’)
ax3.axis[‘right3’].major_ticks.set_color(‘violet’)
ax1.axis[‘right’].major_ticklabels.set_color(‘peru’)
ax2.axis[‘right2’].major_ticklabels.set_color(‘lime’)
ax3.axis[‘right3’].major_ticklabels.set_color(‘violet’)
ax1.axis[‘right’].line.set_color(‘peru’)
ax2.axis[‘right2’].line.set_color(‘lime’)
ax3.axis[‘right3’].line.set_color(‘blue’)

plt.title(‘Arctic Oscillation Index for DJF’, fontsize=20)
plt.show()

 

分析AO数据,取DJF数据段,并画出多y轴图

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

plt.rcParams.update({‘font.family’:’monospace’})

data = pd.read_csv(r”E:\python\44\AO.txt”, sep=’\s+’, header=None, names=[‘year’,’month’,’AO’])
data.index = pd.date_range(“1950-01-01″,”2019-12-31”,freq=’M’)
AO = data.resample(‘Y’).mean()

yearly_avg = np.mean(AO.AO,axis=0)
ano = (AO.AO – yearly_avg)
std = (AO.AO.std())
nor = ((AO.AO – yearly_avg)/std)

fig = plt.figure(figsize=(12,8),dpi=200)
ax1 = fig.add_subplot(1,1,1)
ax1.set_xlabel(‘Year’,font={‘size’:’18’})
ax1.plot(np.arange(1950,2020,1),ano,’ko-‘,lw=’3′,c=’r’,label=’Annual anomaly values’,alpha=0.5)
ax1.set_ylim(-1.5,1.5)
ax1.set_ylabel(‘Anomaly’,font={‘size’:’18’})
plt.legend(prop={‘size’:10},loc=’upper left’)

ax2=ax1.twinx()
ax2.bar(np.arange(1950,2020,1),nor,width=0.4,color=’c’,label=’Normalized values’)
ax2.axhline(0,c=’k’)
ax2.set_ylim(-3,3)
ax2.set_ylabel(‘Normalized’, font={‘size’ :’18’})
plt.legend(prop={‘size’:10},loc=’upper right’)
plt.title(“Arctic Oscillation Index’s Anomalya and Normalized stats”, fontsize=14)
plt.show()

 

画出AO距平序列与标准化序列,折线图、柱状图结合

衍生计算AO冬季的距平序列以及标准化序列,只要添加一个winter mask就行

# 筛选冬季数据(12、1、2月份)
winter_mask = (month >= 12) | (month <= 2)
winter_values = AO[winter_mask]
winter_years = year[winter_mask]

计算AO年序列的平均值、标准差、方差,并挑出大于或小于一倍标准差的年份

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# 从txt文件中加载数据
data = pd.read_csv(r”E:\python\44\AO.txt”,
sep=’\s+’, header=None, names=[‘year’,’month’,’AO’])
data.index = pd.date_range(“1950-01-01″,”2019-12-31″,freq=’M’)
AO = data.resample(‘Y’).mean()

std_deviation = np.std(AO.AO)
variance = np.var(AO.AO)
mean = np.mean(AO.AO)

print(f”标准差:{std_deviation}”)
print(f”方差:{variance}”)
print(f”平均值:{mean}”)

greater_than_std = AO[AO.AO > mean + std_deviation]
less_than_std = AO[AO.AO < mean – std_deviation]

print(f”大于一倍标准差的年份:{greater_than_std}”)
print(f”小于一倍标准差的年份:{less_than_std}”)

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注