需要用到的库
import xarray as xr #数据读取
import numpy as np #用于计算
import matplotlib.pyplot as plt #数据可视化
import cartopy.crs as ccrs #地图投影
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
nc数据的读取
file_name = 'd:/hgt.mon.mean.nc'
file = xr.open_dataset(file_name)
#print(file)
var=file["hgt"]
var_=var.loc['1980-01':'2010-12']
hgt=var_[6::12,2,:,:]
取平均
mean=np.nanmean(hgt,axis=0)
数组的加减
例如:纬偏图中,先要将lon_mean转为竖着的,然后用二维的数组减去一维的
lon_ano=var_1[0,2,:,:]-lon_mean.reshape(-1,1)
画图
fig=plt.figure(dpi=600)
#ax1 = fig.add_subplot(111)
ax1 = plt.subplot(111, projection=ccrs.PlateCarree(central_longitude=180))
#ax1.set_global() #使得轴域(Axes即两条坐标轴围城的区域)适应地图的大小
ax1.coastlines() #画出海岸线
#坐标轴
ax1.set_xticks([0, 60, 120, 180, 240, 300, 360], crs=ccrs.PlateCarree())
ax1.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
lon_formatter = LongitudeFormatter()
lat_formatter = LatitudeFormatter()
#加°E等
ax1.xaxis.set_major_formatter(lon_formatter)
ax1.yaxis.set_major_formatter(lat_formatter)
#cmap = plt.get_cmap('bwr') #选择色标
plt.contourf(lon,lat,ano,cmap='Spectral_r',linewidth=.5,transform=ccrs.PlateCarree())
plt.colorbar(shrink=1,orientation = 'horizontal')#(使色条横向)
ax1.set_title('850hPa hgt 2015_07 ano',)
cs = plt.contour(lon,lat,ano,colors='k',transform=ccrs.PlateCarree()) #绘制填色图。颜色设置为黑色
plt.clabel(cs,fmt = '%1.0f',fontsize = 7) #添加等值线标签,不保留小数
plt.savefig('d:/Jul_850_ano.png') #保存图片