使用数据:slp.mon.ltm.1991-2020.nc
使用库:Matplotlib、NumPy、netCDF4、Cartopy
彩色图像
import matplotlib.pyplot as plt
import netCDF4 as nc
import numpy as np
import cartopy.mpl.ticker as cmt
import cartopy.crs as ccrs
import matplotlib.path as mpath
import cartopy.feature as cfeature
from cartopy.util import add_cyclic_point
from cartopy.mpl.ticker import (LongitudeFormatter, LatitudeFormatter)
import matplotlib as mpl
import matplotlib.ticker as mticker
plt.rcParams['font.sans-serif']= ['Microsoft YaHei'] # 设置“微软雅黑”,图上显示出中文
plt.rcParams['axes.unicode_minus'] = False # 设置中文后,解决坐标轴上负号显示问题
para_level = 1000
f = nc.Dataset(r"slp.mon.ltm.1991-2020.nc",'r')
lat = f.variables['lat'][:]
lon0 = f.variables['lon'][:]
time = nc.num2date(f.variables['time'][:],f.variables['time'].units).data
months = np.array([i.month for i in time])
def pa(para_month):
slp = f.variables['slp'][:]
index_month = np.where(months==para_month)[0][0]
slp = slp[index_month]
slp, lon = add_cyclic_point(slp, coord=lon0)
Lon, Lat = np.meshgrid(lon,lat)
return Lon,Lat,slp
Lon1,Lat1,slp1=pa(1)
Lon2,Lat2,slp2=pa(7)
fig = plt.figure(figsize=(11,14))
#001
ax = plt.subplot(211,projection=ccrs.PlateCarree(central_longitude=180))
ax.set_extent([-180,180,-90,90])
levels = np.arange(970,1060,5)
ec = ax.contourf(Lon1-180,Lat1, slp1,cmap = 'RdBu_r',levels=levels)
ac = ax.contour(Lon1-180,Lat1, slp1,levels=levels,colors='black',linewidths=1,linestyles='-')
fig.colorbar(ec)
ax.clabel(ac)
ax.set_xticks([-180,-150,-120,-90,-60,-30,0,30,60,90,120,150,180])
ax.set_yticks([-90,-60,-30,0,30,60,90])
ax.xaxis.set_major_formatter(LongitudeFormatter())
ax.yaxis.set_major_formatter(LatitudeFormatter())
ax.add_feature(cfeature.COASTLINE)
ax.tick_params(axis='both',which='major',labelsize=10,direction='out',length=5,width=0.8)
ax.minorticks_on()
ax.tick_params(axis='both',which='minor',direction='out',width=0.8,top=True,right=True)
plt.title("1月")
#002
ax = plt.subplot(212,projection=ccrs.PlateCarree(central_longitude=180))
ax.set_extent([-180,180,-90,90])
levels = np.arange(970,1060,5)
ec = ax.contourf(Lon2-180,Lat2, slp2,cmap = 'RdBu_r',levels=levels)
ac = ax.contour(Lon2-180,Lat2, slp2,levels=levels,colors='black',linewidths=1,linestyles='-')
fig.colorbar(ec)
ax.clabel(ac)
ax.set_xticks([-180,-150,-120,-90,-60,-30,0,30,60,90,120,150,180])
ax.set_yticks([-90,-60,-30,0,30,60,90])
ax.xaxis.set_major_formatter(LongitudeFormatter())
ax.yaxis.set_major_formatter(LatitudeFormatter())
ax.add_feature(cfeature.COASTLINE)
ax.tick_params(axis='both',which='major',labelsize=10,direction='out',length=5,width=0.8)
ax.minorticks_on()
ax.tick_params(axis='both',which='minor',direction='out',width=0.8,top=True,right=True)
plt.title("7月")
plt.suptitle("图4-1-5 平均海平面气压场(单位:hPa)",fontsize='xx-large')
plt.savefig("图4-1-5.png",dpi=800)
plt.show()
黑白图像
import matplotlib.pyplot as plt
import netCDF4 as nc
import numpy as np
import cartopy.mpl.ticker as cmt
import cartopy.crs as ccrs
import matplotlib.path as mpath
import cartopy.feature as cfeature
from cartopy.util import add_cyclic_point
from cartopy.mpl.ticker import (LongitudeFormatter, LatitudeFormatter)
import matplotlib as mpl
import matplotlib.ticker as mticker
plt.rcParams['font.sans-serif']= ['Microsoft YaHei'] # 设置“微软雅黑”,图上显示出中文
plt.rcParams['axes.unicode_minus'] = False # 设置中文后,解决坐标轴上负号显示问题
para_level = 1000
f = nc.Dataset(r"slp.mon.ltm.1991-2020.nc",'r')
lat = f.variables['lat'][:]
lon0 = f.variables['lon'][:]
time = nc.num2date(f.variables['time'][:],f.variables['time'].units).data
months = np.array([i.month for i in time])
def pa(para_month):
slp = f.variables['slp'][:]
index_month = np.where(months==para_month)[0][0]
slp = slp[index_month]
slp, lon = add_cyclic_point(slp, coord=lon0)
Lon, Lat = np.meshgrid(lon,lat)
return Lon,Lat,slp
Lon1,Lat1,slp1=pa(1)
Lon2,Lat2,slp2=pa(7)
fig = plt.figure(figsize=(11,14))
#001
ax = plt.subplot(211,projection=ccrs.PlateCarree(central_longitude=180))
ax.set_extent([-180,180,-90,90])
levels = np.arange(970,1060,5)
ac = ax.contour(Lon1-180,Lat1, slp1,levels=levels,colors='black',linewidths=1,linestyles='-')
ax.clabel(ac)
ax.set_xticks([-180,-150,-120,-90,-60,-30,0,30,60,90,120,150,180])
ax.set_yticks([-90,-60,-30,0,30,60,90])
ax.xaxis.set_major_formatter(LongitudeFormatter())
ax.yaxis.set_major_formatter(LatitudeFormatter())
ax.add_feature(cfeature.COASTLINE)
ax.tick_params(axis='both',which='major',labelsize=10,direction='out',length=5,width=0.8)
ax.minorticks_on()
ax.tick_params(axis='both',which='minor',direction='out',width=0.8,top=True,right=True)
plt.title("1月")
#002
ax = plt.subplot(212,projection=ccrs.PlateCarree(central_longitude=180))
ax.set_extent([-180,180,-90,90])
levels = np.arange(970,1060,5)
ac = ax.contour(Lon2-180,Lat2, slp2,levels=levels,colors='black',linewidths=1,linestyles='-')
ax.clabel(ac)
ax.set_xticks([-180,-150,-120,-90,-60,-30,0,30,60,90,120,150,180])
ax.set_yticks([-90,-60,-30,0,30,60,90])
ax.xaxis.set_major_formatter(LongitudeFormatter())
ax.yaxis.set_major_formatter(LatitudeFormatter())
ax.add_feature(cfeature.COASTLINE)
ax.tick_params(axis='both',which='major',labelsize=10,direction='out',length=5,width=0.8)
ax.minorticks_on()
ax.tick_params(axis='both',which='minor',direction='out',width=0.8,top=True,right=True)
plt.title("7月")
plt.suptitle("图4-1-5 平均海平面气压场(单位:hPa)",fontsize='xx-large')
plt.savefig("图4-1-5(黑白).png",dpi=800)
plt.show()