在 Python 中使用 cartopy 自定义多个子图

Customize multiple subplots with cartopy in Python

我尝试使用 cartopy 使用子图自定义更长垂直尺寸的图,但仍然无法弄清楚如何增加每个图的高度(垂直高度)。

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import xarray as xr
import numpy as np
import matplotlib.ticker as mticker
# Load available datasets
ds=xr.tutorial.load_dataset("air_temperature")
# Get 6 maps (images)
air=[ds.air.isel(time=i) for i in range(7)]
# Specify crs
proj = ccrs.PlateCarree() #ccrs.Orthographic()
# Set size and number of plots
fig,axes = plt.subplots(ncols=3,nrows=2,figsize=(20,7),
                      subplot_kw={'projection': proj},gridspec_kw = {'wspace':0.2, 'hspace':0.007})

# Zip maps and axes and index
mzip=zip(air,[y for x in axes for y in x])
# Loop over the maps and axes
for img, ax in mzip:
        # Add countries
    ax.add_feature(cfeature.BORDERS, facecolor="green")
    # plot maps/images
    temp=img.plot.contourf(ax=ax,transform=proj,cmap="magma",vmax=300,vmin=220, add_colorbar=False)
    # Create gridlines
    gl = ax.gridlines(crs=proj, linewidth=1, color='black', alpha=0.2, linestyle="--")
    # Manipulate gridlines number and spaces
    gl.ylocator = mticker.FixedLocator(np.arange(-90,90,20))
    gl.xlocator = mticker.FixedLocator(np.arange(-180, 180, 25)) 
    gl.xlabels_top = False
    gl.xlabels_bottom = True
    gl.ylabels_left = True
    gl.ylabels_right = False

plt.show()

您可以在创建子图时添加 aspect 关键字。但正如 Jody Klymak 已经提到的那样,也许选择不同的投影会更好?

但添加例如:subplot_kw={'projection': proj, "aspect": 2}

我稍微改变了你的例子:

ds = xr.tutorial.load_dataset("air_temperature")
air = [ds.air.isel(time=i) for i in range(7)]

proj = ccrs.PlateCarree()
fig,axes = plt.subplots(
    2, 3, figsize=(9, 6), sharex=True, sharey=True,
    subplot_kw={'projection': proj, "aspect": 2},
    gridspec_kw = {'wspace':0.2, 'hspace':0.007},
)

for i, (img, ax) in enumerate(zip(air,[y for x in axes for y in x])):

    temp = img.plot.contourf(
        ax=ax, transform=proj, cmap="magma",
        vmax=300,vmin=220, add_colorbar=False,
    )
    ax.set_title(img.time.to_dict()["data"])
    ax.add_feature(cfeature.BORDERS, facecolor="green")
    
    gl = ax.gridlines(crs=proj, linewidth=1, color='black', alpha=0.2, linestyle="--")

    gl.ylocator = mticker.FixedLocator(np.arange(-90,90,20))
    gl.xlocator = mticker.FixedLocator(np.arange(-180, 180, 25))
    
    if i>2:
        gl.bottom_labels = True
    if i == 0 or i == 3:
        gl.left_labels = True

结果: