python基于MERRA2的
nc数据绘制空间分布图
---文末附完整代码
本文作者:第八星系-刘术辉
联系邮箱:1211284952@qq.com
import netCDF4 as nc
import numpy as np
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import matplotlib.pyplot as plt
from cartopy.io.shapereader import Reader
import cartopy.feature as cfeature
import cmaps
shap=Reader('SCmap.shp').geometries()
sichuan = cfeature.ShapelyFeature(shap,crs=ccrs.PlateCarree(),edgecolor='k', facecolor='none') #读取地图文件
fig = plt.figure(figsize=(12,8),dpi=300) #设置画布
file="MERRA2_400.inst3_2d_gas_Nx.20180521.SUB.nc"
dataset = nc.Dataset(file)
AODANA = dataset.variables['AODANA'][:]#读取nc数据中的AODANA
var_data = np.array(AODANA)#将读取的数据转为ndarray类型使其满足contourf函数
print(type(var_data))
avevar_data = var_data.mean(0)
print(avevar_data.shape)
#格点17*18
lat = np.arange(26,34.5,0.5)
lon = np.arange(97.5,108.75,0.625)#经纬度设置格点范围和间隔
ax = fig.add_subplot(111,projection=ccrs.PlateCarree())
ax.add_feature(sichuan)
# ax.set_extent([97, 108.3, 26, 34.5], crs=ccrs.PlateCarree())
#ax.set_xticks([97,100,103,106,109], crs=ccrs.PlateCarree())
ax.set_extent([97.5, 108.125, 26, 34], crs=ccrs.PlateCarree())#设置经纬度矩形范围
ax.set_xticks([98,101,104,107], crs=ccrs.PlateCarree())#设置x轴刻度
ax.set_yticks([26,30,34], crs=ccrs.PlateCarree())
lon_formatter = LongitudeFormatter()
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
b = ax.contourf(lon,lat,avevar_data,cmap=cmaps.rainbow)#绘制等值线图
plt.colorbar(b,orientation='horizontal',label='Aerosol Optical Depth',shrink=0.8)#添加颜色条
plt.subplots_adjust(right=0.8)
# plt.show()
plt.savefig('20180521.png')
import netCDF4 as nc
import numpy as np
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import matplotlib.pyplot as plt
from cartopy.io.shapereader import Reader
import cartopy.feature as cfeature
import cmaps
shap=Reader('SCmap.shp').geometries()
sichuan = cfeature.ShapelyFeature(shap,crs=ccrs.PlateCarree(),edgecolor='k', facecolor='none') #读取地图文件
fig = plt.figure(figsize=(12,8),dpi=300) #设置画布
file = "MERRA2_400.inst3_2d_gas_Nx.20180521.SUB.nc"
dataset = nc.Dataset(file)
AODANA = dataset.variables['AODANA'][:]#读取nc数据中的AODANA
var_data = np.array(AODANA)#将读取的数据转为ndarray类型使其满足contourf函数
print(type(var_data))
avevar_data = var_data.mean(0)
print(avevar_data.shape)
#格点17*18
lat = np.arange(26,34.5,0.5)
lon = np.arange(97.5,108.75,0.625)#经纬度设置格点范围和间隔
ax = fig.add_subplot(111,projection=ccrs.PlateCarree())
ax.add_feature(sichuan)
# ax.set_extent([97, 108.3, 26, 34.5], crs=ccrs.PlateCarree())
# ax.set_xticks([97,100,103,106,109], crs=ccrs.PlateCarree())
ax.set_extent([97.5, 108.125, 26, 34], crs=ccrs.PlateCarree())#设置经纬度矩形范围
ax.set_xticks([98,101,104,107], crs=ccrs.PlateCarree())#设置x轴刻度
ax.set_yticks([26,30,34], crs=ccrs.PlateCarree())
lon_formatter = LongitudeFormatter()
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
b = ax.contourf(lon,lat,avevar_data,cmap=cmaps.rainbow)#绘制等值线图
plt.colorbar(b,orientation='horizontal',label='Aerosol Optical Depth',shrink=0.8)#添加颜色条
plt.subplots_adjust(right=0.8)
# plt.show()
plt.savefig('20180521.png')
未完待续~
本文编辑:myp