【问题标题】:align a cartopy 2D map plot with a 1D line plot将 cartopy 2D 地图图与 1D 线图对齐
【发布时间】:2020-07-03 13:03:23
【问题描述】:

我需要使用以下代码将 cartopy 2D 地图子图与 1D 线子图对齐:

import xarray as xr
import cartopy.crs as ccrs
import matplotlib.pyplot as plt


air = xr.tutorial.open_dataset('air_temperature').air[0]

fig = plt.figure(figsize=(14,5))

ax1 = fig.add_subplot(121,
                 projection=ccrs.PlateCarree(central_longitude=180))
air.plot.pcolormesh(ax=ax1,
               transform=ccrs.PlateCarree(),
               cbar_kwargs={'orientation':'horizontal'})
ax1.coastlines()
ax1.set_extent((air.lon[0], air.lon[-1], air.lat[-1], air.lat[0]),
               crs=ccrs.PlateCarree())

ax2 = fig.add_subplot(122)
air.mean('lon').plot(ax=ax2, y='lat')

plt.tight_layout()

这给出了情节:

两个子图具有相同的 y-(纬度-)范围,我希望两个轴的框可以相互对齐。有没有简单的方法来做到这一点?

【问题讨论】:

    标签: matplotlib python-xarray cartopy


    【解决方案1】:

    我从proplot找到了一个不错的解决方案:

    import xarray as xr
    import proplot as plot
    
    air = xr.tutorial.open_dataset('air_temperature').air[0]
    
    array = [
        [1, 1, 1, 2],
        [1, 1, 1, 2],
        [3, 3, 3, 0]
    ]
    
    fig, ax = plot.subplots(array, proj={1:'pcarree'}, width=7)
    
    air.plot(ax=ax[0], add_colorbar=False)
    air.mean('lon').plot(ax=ax[1], y='lat', xincrease=True)
    air.mean('lat').plot(ax=ax[2])
    
    ax.format(abc=True, abcloc='l', abcstyle='(a)')
    
    ax[0].format(lonlim=(air.lon[0], air.lon[-1]), latlim=(air.lat[0], air.lat[-1]),
                 coast=True, labels=True, lonlines=20, latlines=10,
                 title='temperature')
    ax[1].set(title='zonal mean', xlim=[240, 300], ylabel=None,
              yticks=[20, 30, 40, 50, 60, 70],
              yticklabels=['20°N','30°N','40°N','50°N','60°N','70°N'])
    ax[2].set(title='meridional mean', ylim=[260, 285],
              ylabel=None, xlabel='longitude',
              xticklabels=['160°W','140°W','120°W','100°W','80°W','60°W','40°W'])
    

    这给出了这个情节:

    但是,为面板 a 添加ylabel='latitude (N)' 仍然存在问题。但这对我来说已经足够了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-31
      • 1970-01-01
      • 2015-01-16
      • 2015-08-27
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      相关资源
      最近更新 更多