一键绘制Nature风格全球地图

参考大佬文章
先来欣赏一些全球地图,非常美观啊。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
上面的图,都是用Python绘制的,有的还有显著性划线(或显著性打点),但是对于Cartopy来说,这些操作需要很多很多的代码,如果能写成函数,一键绘制就好了。

安装必要的环境,这里主要用了一个mplotutils库,是GitHub下载的:

import os
os.chdir('/home/mw/project/mplotutils-main')
pip install .
Processing /home/mw/project/mplotutils-main
  DEPRECATION: A future pip version will change local packages to be built in-place without first copying to a temporary directory. We recommend you use --use-feature=in-tree-build to test your packages with this new behavior before it becomes the default.
   pip 21.3 will remove support for this functionality. You can find discussion regarding this at https://github.com/pypa/pip/issues/7555.
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... done
    Preparing wheel metadata ... done
Requirement already satisfied: setuptools>=40.4 in /opt/conda/lib/python3.9/site-packages (from mplotutils==0.3.1.post1.dev16+gc9adcb5) (49.6.0.post20210108)
Requirement already satisfied: matplotlib>=3.4 in /opt/conda/lib/python3.9/site-packages (from mplotutils==0.3.1.post1.dev16+gc9adcb5) (3.7.2)
Requirement already satisfied: numpy>=1.17 in /opt/conda/lib/python3.9/site-packages (from mplotutils==0.3.1.post1.dev16+gc9adcb5) (1.24.4)
Requirement already satisfied: cartopy>=0.18 in /opt/conda/lib/python3.9/site-packages (from mplotutils==0.3.1.post1.dev16+gc9adcb5) (0.22.0)
Requirement already satisfied: shapely>=1.7 in /opt/conda/lib/python3.9/site-packages (from cartopy>=0.18->mplotutils==0.3.1.post1.dev16+gc9adcb5) (1.8.0)
Requirement already satisfied: pyproj>=3.1.0 in /opt/conda/lib/python3.9/site-packages (from cartopy>=0.18->mplotutils==0.3.1.post1.dev16+gc9adcb5) (3.2.1)
Requirement already satisfied: packaging>=20 in /opt/conda/lib/python3.9/site-packages (from cartopy>=0.18->mplotutils==0.3.1.post1.dev16+gc9adcb5) (23.1)
Requirement already satisfied: pyshp>=2.1 in /opt/conda/lib/python3.9/site-packages (from cartopy>=0.18->mplotutils==0.3.1.post1.dev16+gc9adcb5) (2.1.3)
Requirement already satisfied: contourpy>=1.0.1 in /opt/conda/lib/python3.9/site-packages (from matplotlib>=3.4->mplotutils==0.3.1.post1.dev16+gc9adcb5) (1.1.1)
Requirement already satisfied: python-dateutil>=2.7 in /opt/conda/lib/python3.9/site-packages (from matplotlib>=3.4->mplotutils==0.3.1.post1.dev16+gc9adcb5) (2.8.1)
Requirement already satisfied: fonttools>=4.22.0 in /opt/conda/lib/python3.9/site-packages (from matplotlib>=3.4->mplotutils==0.3.1.post1.dev16+gc9adcb5) (4.42.1)
Requirement already satisfied: cycler>=0.10 in /opt/conda/lib/python3.9/site-packages (from matplotlib>=3.4->mplotutils==0.3.1.post1.dev16+gc9adcb5) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /opt/conda/lib/python3.9/site-packages (from matplotlib>=3.4->mplotutils==0.3.1.post1.dev16+gc9adcb5) (1.3.1)
Requirement already satisfied: pyparsing<3.1,>=2.3.1 in /opt/conda/lib/python3.9/site-packages (from matplotlib>=3.4->mplotutils==0.3.1.post1.dev16+gc9adcb5) (2.4.7)
Requirement already satisfied: importlib-resources>=3.2.0 in /opt/conda/lib/python3.9/site-packages (from matplotlib>=3.4->mplotutils==0.3.1.post1.dev16+gc9adcb5) (5.4.0)
Requirement already satisfied: pillow>=6.2.0 in /opt/conda/lib/python3.9/site-packages (from matplotlib>=3.4->mplotutils==0.3.1.post1.dev16+gc9adcb5) (8.4.0)
Requirement already satisfied: six in /opt/conda/lib/python3.9/site-packages (from cycler>=0.10->matplotlib>=3.4->mplotutils==0.3.1.post1.dev16+gc9adcb5) (1.16.0)
Requirement already satisfied: zipp>=3.1.0 in /opt/conda/lib/python3.9/site-packages (from importlib-resources>=3.2.0->matplotlib>=3.4->mplotutils==0.3.1.post1.dev16+gc9adcb5) (3.4.1)
Requirement already satisfied: certifi in /opt/conda/lib/python3.9/site-packages (from pyproj>=3.1.0->cartopy>=0.18->mplotutils==0.3.1.post1.dev16+gc9adcb5) (2023.7.22)
Building wheels for collected packages: mplotutils
  Building wheel for mplotutils (PEP 517) ... done
  Created wheel for mplotutils: filename=mplotutils-0.3.1.post1.dev16+gc9adcb5-py3-none-any.whl size=40411 sha256=5daa2d3b818d263b306f5b99788de7f69be68764d6656257baf16a4e007b39a2
  Stored in directory: /home/mw/.cache/pip/wheels/4b/fe/7f/88825902fe7a6595152a7451e5f7f5f52092c9f3b033946a85
Successfully built mplotutils
Installing collected packages: mplotutils
Successfully installed mplotutils-0.3.1.post1.dev16+gc9adcb5
Note: you may need to restart the kernel to use updated packages.
os.chdir('/home/mw/project')
import plot

加载我的测试数据:
这里我把数据的Coordinates重命名了,数据的Coordinates:必须包含lon和lat属性,若没有需要重命名,这里我的基本绘图数据单位是xarray.DataArray类型,如果你是numpy.ndarray类型,需要转换为我的标准类型。

import xarray as xr
file_name='/home/mw/project/ERA5temp_1978_monthly.nc' 
ds=xr.open_dataset(file_name)
lat = ds['latitude']
lon = ds['longitude']


ds = ds.swap_dims({'latitude':'lat','longitude':'lon'})
ds.coords['lat'] = ('lat', lat.to_numpy())
ds.coords['lon'] = ('lon', lon.to_numpy()) # 对维度lon指定新的坐标信息lon
ds = ds.reset_coords(names=['latitude','longitude'], drop=True)
ds['t2m'] = ds['t2m'] - 273.15
ds['t2m']
/opt/conda/lib/python3.9/site-packages/xarray/backends/plugins.py:71: RuntimeWarning: Engine 'cfradial1' loading failed:
cannot import name 'ParasiteAxesAuxTrans' from 'mpl_toolkits.axisartist' (/opt/conda/lib/python3.9/site-packages/mpl_toolkits/axisartist/__init__.py)
  warnings.warn(f"Engine {name!r} loading failed:\n{ex}", RuntimeWarning)

在这里插入图片描述

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np
fig = plt.figure()
proj = ccrs.PlateCarree() #ccrs.Robinson()ccrs.Mollweide()Mollweide()
ax = fig.add_subplot(111, projection=proj)
levels = np.linspace(-30, 30, num=19)
plot.one_map_flat(ds['t2m'][0], ax, levels=levels, cmap="BrBG_r", mask_ocean=False, add_coastlines=True, add_land=False, plotfunc="pcolormesh")

在这里插入图片描述

#import rioxarray as xrx
#p = rxr.open_rasterio(filename)
p = np.mean(ds['t2m'], 0) > -20
fig = plt.figure()  
proj = ccrs.PlateCarree()  #ccrs.Robinson()  
#proj = ccrs.Robinson()  
ax = fig.add_subplot(111, projection=proj)
levels = np.linspace(-30, 30, num=19)
plot.one_map(ds['t2m'], ax,  average='mean', dim='time', cmap="RdBu_r", levels=levels,  mask_ocean=True,  add_coastlines=True,  add_land=True,  plotfunc="pcolormesh", colorbar=True, getmean=True)
plot.hatch_map(ax, p, 3 * ".", label="Lack of model agreement", invert=True, linewidth=0.25, color="0.1")

在这里插入图片描述
有了上述基础的函数,结合python绘图语法,就可以定制自己的函数了:

如果我们想复现这个IPCC的多栏图:
在这里插入图片描述
可以自己定制一个三栏绘图函数,就利用上述的基础功能:

at_warming_c = []
at_warming_c.append(ds['t2m'][5:8])
at_warming_c.append(ds['t2m'][9:12])
at_warming_c.append(ds['t2m'][0:3])
len(at_warming_c)

#fig = plt.figure()   
#proj = ccrs.Robinson()  
#
#ax = fig.add_subplot(131, projection=proj)
plot.at_warming_level_one(at_warming_c=at_warming_c, unit="Change (times as frequent)", title='drought frequency change w.r.t. 1850-1900', \
                     average="median",  mask_ocean=True,  colorbar=True, cmap="RdBu",  dim='time', add_legend=False, hatch_data=None, levels=levels, plotfunc='pcolormesh', getmean=True)

在这里插入图片描述
参考

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值