python 全球降水四季空间分布图 居中经线问题

python 全球降水四季空间分布图 居中经线问题,第1张

# -*- 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('GPCP.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')# 改动了居中经线后,横坐标的经纬度维持不变!!!后续的绘图要根据原始的-180~180的横坐标绘制
        ax.coastlines()
        # 消除白线
        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-180, cycle_LAT, cycle_data,
                         cmap='bwr_r', extend='both',
                         levels=range(0, 12))
        plt.title(title[i])
        plt.plot(range(-180, 180), [0] * 360, 'k--')
        for line in [9-180, 43.5-180, 104-180, 162.5-180, 220.5-180, 280-180, 311-180]:
            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'],
                   fontsize=10)
        plt.yticks(range(-90, 91, 30), ['90°S', '60°S', '30°S', '0', '30°N', '60°N', '90°N'],
                   fontsize=10)
        plt.colorbar(cs)
    plt.show()


注意:

  1. 改动了居中经线后,横坐标的经纬度维持不变!!!依旧是-180~180°
  2. 降水分布图也要跟着向西平移180度

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/868483.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存