0
点赞
收藏
分享

微信扫一扫

python 全球降水四季空间分布图

上古神龙 2022-04-30 阅读 72
# -*- coding: utf-8 -*-
"""
@Features: 空间分布图 季节平均
@Date:2022/4/30
"""
import matplotlib.pyplot as plt
import numpy as np
import netCDF4 as nc
import cartopy.crs as ccrs
from cartopy.util import add_cyclic_point


def sea_mean(start):
    temp = []
    for i in range(start, 468, 12):
        temp.append(i)
        temp.append(i + 1)
        temp.append(i + 2)
    mean_season = np.nanmean(pre[temp, :, :], 0)
    return mean_season


if __name__ == '__main__':
    dataset = nc.Dataset('CMAP.precip.mon.mean.nc')  # 读取数据
    print(dataset.variables.keys())  # 输出所有变量
    lon = dataset.variables['lon'][:].data  # 读取经度
    lat = dataset.variables['lat'][:].data  # 读取维度
    time = dataset.variables['time']  # 读取时间
    real_time = nc.num2date(time, time.units).data  # 转成时间格式
    pre = dataset.variables['precip'][:].data  # 读取降水
    pre[pre < 0] = np.nan
    # 绘图
    fig = plt.figure()  # 设置一个画板,将其返还给fig
    for i in range(4):
        mon = [3, 6, 9, 12]
        title = ['MAM', 'JAS', 'SON', 'DJS']
        ax = fig.add_subplot(2, 2, i + 1,
                             projection=ccrs.PlateCarree(central_longitude=180),
                             facecolor='gray')
        ax.coastlines()
        # 去除nc数据导致的白线
        cycle_data, cycle_lon = add_cyclic_point(sea_mean(mon[i]), coord=lon)
        cycle_LON, cycle_LAT = np.meshgrid(cycle_lon, lat)
        cs = ax.contourf(cycle_LON, cycle_LAT, cycle_data,
                         cmap='RdBu', extend='both',
                         levels=range(0, 18))
        plt.title(title[i])
        plt.plot(range(-180, 180), [0] * 360, 'k--')
        for line in [9, 43.5, 104, 162.5, 220.5 - 360, 280 - 360, 311 - 360]:
            plt.plot([line] * 180, range(-90, 90), c='w')
        plt.xticks(range(-180, 180, 60), [0, '60°E', '120°E', '180°', '120°W', '60°W'])
        plt.yticks(range(-90, 91, 30), ['90°S', '60°S', '30°S', '0', '30°N', '60°N', '90°N'])
        plt.colorbar(cs)
    plt.show()

在这里插入图片描述
注意: 不知什么原因,plt.xticks(range(-180, 180, 60), [0, ‘60°E’, ‘120°E’, ‘180°’, ‘120°W’, ‘60°W’])的实际经纬度与地图经纬度不对应,因此,自定义[0, ‘60°E’, ‘120°E’, ‘180°’, ‘120°W’, ‘60°W’]

举报

相关推荐

0 条评论