第八星系人气爱 发表于 2024-2-29 23:41:04

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(, crs=ccrs.PlateCarree())

#ax.set_xticks(, crs=ccrs.PlateCarree())

ax.set_extent(, crs=ccrs.PlateCarree())#设置经纬度矩形范围

ax.set_xticks(, crs=ccrs.PlateCarree())#设置x轴刻度

ax.set_yticks(, 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(, crs=ccrs.PlateCarree())
# ax.set_xticks(, crs=ccrs.PlateCarree())
ax.set_extent(, crs=ccrs.PlateCarree())#设置经纬度矩形范围
ax.set_xticks(, crs=ccrs.PlateCarree())#设置x轴刻度
ax.set_yticks(, 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')

微信搜索“第八星系人造大气理论爱好者”公众号,关注获取文章数据

继续宠爱 发表于 2024-4-27 15:42:20

你怎么看待未来这个技术的发展趋势?
页: [1]
查看完整版本: python基于MERRA2的nc数据绘制空间分布图