天气学原理插图复现(一)——平均温度场经向剖面图

使用数据:air.mon.ltm.1991-2020.nc、pres.tropp.mon.ltm.1991-2020.nc

使用库:Matplotlib、NumPy、netCDF4、Cartopy

彩色图像 

天气学原理插图复现(一)——平均温度场经向剖面图_第1张图片

import matplotlib.pyplot as plt
import netCDF4 as nc
import numpy as np
import cartopy.mpl.ticker as cmt

plt.rcParams['font.sans-serif']= ['Microsoft YaHei'] # 设置“微软雅黑”,图上显示出中文
plt.rcParams['axes.unicode_minus'] = False # 设置中文后,解决坐标轴上负号显示问题
para_month = 1

## 等温大气模型
Rd = 287
Tv = 273
g = 9.8
p0 = 1013.2
Hp = Rd*Tv/g
def p2z(p):
 '''p:array'''
 return Hp*np.log(p0/p)

##读取数据
f = nc.Dataset(r"air.mon.ltm.1991-2020.nc",'r')
ftro = nc.Dataset(r"pres.tropp.mon.ltm.1991-2020.nc",'r')
tro1 = ftro.variables['pres'][:][0,:,0]
tro2 = ftro.variables['pres'][:][6,:,6]
level = f.variables['level'][:]
lat = f.variables['lat'][:]
temp = f.variables['air'][:]
time = nc.num2date(f.variables['time'][:],f.variables['time'].units).data
months = np.array([i.month for i in time])

##数据处理
z = p2z(level)
ztro1 = p2z(tro1)
ztro2 = p2z(tro2)
Lat, Z = np.meshgrid(lat,z)
fig = plt.figure(figsize=[11,14])

#001
temp1 = temp[0][:,:,0]
ax = plt.subplot(211)
levels = np.arange(-100,60,5)
ec = ax.contourf(Lat,Z, temp1,cmap = 'rainbow',levels=levels)
ac = ax.contour(Lat,Z, temp1,levels=levels,colors='black',linewidths=1,linestyles='-')
ax.plot(lat,ztro1,linewidth=5,linestyle='--',color='red')
fig.colorbar(ec)
ax.clabel(ac)
ax.set_ylabel('level(hPa)')
ax.set_yticks(z[:11],level[:11].astype(int))
LAT=[-90, -60, -30, 0, 30, 60, 90]
ax.set_xticks(LAT)
ax.invert_xaxis()
lat_formatter = cmt.LatitudeFormatter()
ax.xaxis.set_major_formatter(lat_formatter)
ax.xaxis.set_major_formatter(cmt.LatitudeFormatter())
plt.title("1月")

#002
temp2 = temp[6][:,:,0]
ax = plt.subplot(212)
levels = np.arange(-100,60,5)
ec = ax.contourf(Lat,Z, temp2,cmap = 'rainbow',levels=levels)
ac = ax.contour(Lat,Z, temp2,levels=levels,colors='black',linewidths=1,linestyles='-')
ax.plot(lat,ztro2,linewidth=5,linestyle='--',color='red')
fig.colorbar(ec)
ax.clabel(ac)
ax.set_xlabel('Latitude(°)')
ax.set_ylabel('Level(hPa)')
ax.set_yticks(z[:11],level[:11].astype(int))
LAT=[-90, -60, -30, 0, 30, 60, 90]
ax.set_xticks(LAT)
ax.invert_xaxis()
lat_formatter = cmt.LatitudeFormatter()
ax.xaxis.set_major_formatter(lat_formatter)
ax.xaxis.set_major_formatter(cmt.LatitudeFormatter())#给横坐标加经度符号
plt.title("7月")

plt.suptitle("图4-1-1   平均温度场经向剖面图(单位:℃)",fontsize='xx-large')

plt.rcParams['figure.figsize']=(11,14)
plt.savefig("图4-1-1.png",dpi=800)
plt.show()

黑白图像

天气学原理插图复现(一)——平均温度场经向剖面图_第2张图片

import matplotlib.pyplot as plt
import netCDF4 as nc
import numpy as np
import cartopy.mpl.ticker as cmt

plt.rcParams['font.sans-serif']= ['Microsoft YaHei'] # 设置“微软雅黑”,图上显示出中文
plt.rcParams['axes.unicode_minus'] = False # 设置中文后,解决坐标轴上负号显示问题
para_month = 1

## 等温大气模型
Rd = 287
Tv = 273
g = 9.8
p0 = 1013.2
Hp = Rd*Tv/g
def p2z(p):
 '''p:array'''
 return Hp*np.log(p0/p)

f = nc.Dataset(r"air.mon.ltm.1991-2020.nc",'r')
ftro = nc.Dataset(r"pres.tropp.mon.ltm.1991-2020.nc",'r')
tro1 = ftro.variables['pres'][:][0,:,0]
tro2 = ftro.variables['pres'][:][6,:,6]
level = f.variables['level'][:]
lat = f.variables['lat'][:]
temp = f.variables['air'][:]
time = nc.num2date(f.variables['time'][:],f.variables['time'].units).data
months = np.array([i.month for i in time])
z = p2z(level)
ztro1 = p2z(tro1)
ztro2 = p2z(tro2)
Lat, Z = np.meshgrid(lat,z)
fig = plt.figure(figsize=[11,14])

#001
temp1 = temp[0][:,:,0]
ax = plt.subplot(211)
levels = np.arange(-100,5,5)
levels2=np.arange(0,60,5)
ac = ax.contour(Lat,Z, temp1,levels=levels,colors='black',linewidths=1,linestyles='dotted')
ac2 = ax.contour(Lat,Z, temp1,levels=levels2,colors='black',linewidths=1)
ax.plot(lat,ztro1,linewidth=1,linestyle='--',color='black')
#fig.colorbar(ec)
ax.clabel(ac)
ax.clabel(ac2)
ax.set_ylabel('level(hPa)')
ax.set_yticks(z[:11],level[:11].astype(int))
LAT=[-90, -60, -30, 0, 30, 60, 90]
ax.set_xticks(LAT)
ax.invert_xaxis()
lat_formatter = cmt.LatitudeFormatter()
ax.xaxis.set_major_formatter(lat_formatter)
ax.xaxis.set_major_formatter(cmt.LatitudeFormatter())#给横坐标加经度符号
plt.title("1月")

#002
temp2 = temp[6][:,:,0]
ax = plt.subplot(212)
#levels = np.arange(-100,60,5)
ac = ax.contour(Lat,Z, temp2,levels=levels,colors='black',linewidths=1,linestyles='dotted')
ac2 = ax.contour(Lat,Z, temp2,levels=levels2,colors='black',linewidths=1)
ax.plot(lat,ztro2,linewidth=1,linestyle='--',color='black')
#fig.colorbar(ec)
ax.clabel(ac)
ax.clabel(ac2)
ax.set_xlabel('Latitude(°)')
ax.set_ylabel('Level(hPa)')
ax.set_yticks(z[:11],level[:11].astype(int))
LAT=[-90, -60, -30, 0, 30, 60, 90]
ax.set_xticks(LAT)
ax.invert_xaxis()
lat_formatter = cmt.LatitudeFormatter()
ax.xaxis.set_major_formatter(lat_formatter)
ax.xaxis.set_major_formatter(cmt.LatitudeFormatter())#给横坐标加经度符号
plt.title("7月")

plt.suptitle("图4-1-1   平均温度场经向剖面图(单位:℃)",fontsize='xx-large')

plt.rcParams['figure.figsize']=(11,14)
plt.savefig("图4-1-1(黑白).png",dpi=800)
plt.show()

首篇致谢:感谢黄老师的支持以及王子哥、强哥、龙哥的帮助!!!

你可能感兴趣的:(天气学插图复现,python,matplotlib)