Cartopy: Too much vertical space between subplots even with tight_layout

Viewed 40

I have six Cartopy plots with 3 columns and 2 rows with colobars for each columns of plots as coded below:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib import colorbar, colors
import cartopy.crs as ccrs
import cartopy.feature as cf

X   = [9.9576, 11.0621, 12.1844, 9.1307, 8.409, 
       11.0108, 11.0549, 9.8648, 12.5024, 7.8343, 
       10.9308, 11.8134, 10.0792, 8.5584, 10.2759, 
       12.1019, 9.2235, 10.3348, 7.828, 10.9206]

Y   = [49.7704, 47.483, 49.6662, 47.6952, 48.4538, 
       47.8009, 49.503, 48.6656, 48.279, 48.0232, 
       49.0115, 48.3477, 50.224, 49.5063, 47.3984, 
       49.0425, 48.6883, 47.7233, 48.3647, 49.8743]

Z   = [2.3337733, -0.52256856, 0.97157093, 
       -0.50601561,  0.21645241, 0.4011776,
       -1.69798815, 1.3751253, -1.79249769, 
       -0.09767434, 0.96454293, 0.12713469, 
       -0.03112513,  1.36738838, -0.37373591,
       0.40124358, 0.77141455,  0.62646425,  
       0.52960763, -1.16011285]

fig,ax = plt.subplots(ncols=3,nrows=2,figsize=(10,7),
                      subplot_kw={'projection': ccrs.PlateCarree()})


fig.tight_layout()

vmin=min(Z)
vmax=max(Z)

for col in range(3):
    for row in range(2):
        ax[row,col].set_extent([7.5377, 14.0065, 47.221, 50.7131])
        ax[row,col].add_feature(cf.BORDERS)
        ax[row,col].add_feature(cf.STATES,linewidth=0.5)
        ax[row,col].coastlines()
        ax[row,col].set_aspect('equal')
        #fig.tight_layout()
            
        ax[row,col].scatter(X, Y, c=Z,
                        cmap="YlOrRd",
                        edgecolors='black',
                        transform=ccrs.PlateCarree(),vmin=vmin,vmax=vmax)
    
    norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax)
    fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap="YlOrRd"),
                 ax=ax[:, col],
                shrink=0.5)

plt.savefig('stackoverflow.png', format='png',dpi=600)

Even though I set the layout to fig.tight_layout() there is also a crazy amount of vertical space between the plots (see below):

enter image description here

How do I reduce this space and at same time scaling the colobars to the right size?

1 Answers

I was able to find an answer, you have to adjust figsize accordingly. Adjusting to figsize=(13,5) and then setting fig.colorbar(...,shrink=0.7) resolved the issue. See result:

enter image description here

Related