How to Plot Only One Colorbar for Multiple Maps Using Cartopy and AxesGrid?

  matplotlib, Numpy, Pandas, Python

We often need to draw multiple similar maps using the same colorbar. A good way is to use the AxesGrid from the Cartopy package. Below is one of my examples of sharing colorbars for 4 submaps. This example can also answer other questions:

  • How to add ocean, river, lakes,coastline and province border to a map?
  • How to set the longitude and latitude extent of a map?
  • How to format the colorbar of map?
  • How to plot a grid map?
  • How to set the maximum and minimum value for a map?
  • How to add title to a map?
  • How to set cmap for a map?
  • How to save a figure to disk?
import sys
import warnings
if not sys.warnoptions:
    warnings.simplefilter('ignore')
#Display data 
import cartopy
import cartopy.crs as ccrs
from cartopy.mpl.geoaxes import GeoAxes
from cartopy.vector_transform import vector_scalar_to_grid
from matplotlib.axes import Axes
import cartopy.feature as cfeature
from cartopy import config
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
from mpl_toolkits.axes_grid1 import AxesGrid
import matplotlib.pyplot as plt

Ignore other code for preparing data

projection = ccrs.PlateCarree()
provinc_bodr = cartopy.feature.NaturalEarthFeature(category='cultural', 
    name='admin_1_states_provinces_lines', scale='50m', facecolor='none', edgecolor='k')
axes_class = (GeoAxes,
              dict(map_projection=projection))
fig = plt.figure(figsize=(15,15))
#!important, set the colarbar
axgr = AxesGrid(fig, 111, axes_class=axes_class,
                nrows_ncols=(2, 2), #! important,set 2x2 maps
                axes_pad=0.6,
                cbar_location='right',
                cbar_mode='single',
                cbar_pad=0.2,
                cbar_size='3%',
                label_mode='')  # note the empty labe
#!plot the first sub map
ax=axgr[0] #important,0,1,2,3
#*************************************************************
#add ocean, coastline,borders,lakes,rivers,provinc_bodr
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,linewidth=0.3)
ax.add_feature(cfeature.BORDERS, linestyle='-', alpha=0.5)
ax.add_feature(cfeature.LAKES, alpha=0.3)
ax.add_feature(cfeature.RIVERS)
ax.add_feature(provinc_bodr, linestyle='-', linewidth=1, edgecolor="k", zorder=10, alpha=0.5)    
ax.set_title('(a) Risk (RCP2.6,2050s)',fontsize=18)
ax.set_xticks(np.linspace(-100, -70, 5), crs=projection)
ax.set_yticks(np.linspace(35, 65, 5), crs=projection)
ax.set_xlim([-100,-70])
ax.set_ylim([35,65])
lon_formatter = LongitudeFormatter(zero_direction_label=True)
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
p = ax.pcolormesh(X, Y, data1,
                  vmin=-100000, #!important, all submaps should be same
                  vmax=100000,  #!important, all submaps should be same
                  transform=projection,
                  cmap='RdBu')
 
#! Plot the second sub map   
ax=axgr[1]
#*************************************************************
#add ocean, coastline,borders,lakes,rivers,provinc_bodr
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,linewidth=0.3)
ax.add_feature(cfeature.BORDERS, linestyle='-', alpha=0.5)
ax.add_feature(cfeature.LAKES, alpha=0.3)
ax.add_feature(cfeature.RIVERS)
ax.add_feature(provinc_bodr, linestyle='-', linewidth=1, edgecolor="k", zorder=10, alpha=0.5)    
ax.set_title('(b) Risk (RCP2.6,2080s)',fontsize=18)
ax.set_xticks(np.linspace(-100, -70, 5), crs=projection)
ax.set_yticks(np.linspace(35, 65, 5), crs=projection)
ax.set_xlim([-100,-70])
ax.set_ylim([35,65])
lon_formatter = LongitudeFormatter(zero_direction_label=True)
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
p = ax.pcolormesh(X, Y, data2,
                  vmin=-100000,#! important
                  vmax=100000, #! important
                  transform=projection,
                  cmap='RdBu')
#! Plot the third sub maps
ax=axgr[2]  #!important
#*************************************************************
#add ocean, coastline,borders,lakes,rivers,provinc_bodr
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,linewidth=0.3)
ax.add_feature(cfeature.BORDERS, linestyle='-', alpha=0.5)
ax.add_feature(cfeature.LAKES, alpha=0.3)
ax.add_feature(cfeature.RIVERS)
ax.add_feature(provinc_bodr, linestyle='-', linewidth=1, edgecolor="k", zorder=10, alpha=0.5)    
ax.set_title('(c) Risk (RCP6.0,2050s)',fontsize=18)
ax.set_xticks(np.linspace(-100, -70, 5), crs=projection)
ax.set_yticks(np.linspace(35, 65, 5), crs=projection)
ax.set_xlim([-100,-70])
ax.set_ylim([35,65])
lon_formatter = LongitudeFormatter(zero_direction_label=True)
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
p = ax.pcolormesh(X, Y, data3,
                  vmin=-100000, #! important
                  vmax=100000,  #! important
                  transform=projection,
                  cmap='RdBu')
#! Plot the last submap
ax=axgr[3] #!important
#*************************************************************
#add ocean, coastline,borders,lakes,rivers,provinc_bodr
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,linewidth=0.3)
ax.add_feature(cfeature.BORDERS, linestyle='-', alpha=0.5)
ax.add_feature(cfeature.LAKES, alpha=0.3)
ax.add_feature(cfeature.RIVERS)
ax.add_feature(provinc_bodr, linestyle='-', linewidth=1, edgecolor="k", zorder=10, alpha=0.5)    
ax.set_title('(d) Risk:RCP6.0,2080s',fontsize=18)
ax.set_xticks(np.linspace(-100, -70, 5), crs=projection)
ax.set_yticks(np.linspace(35, 65, 5), crs=projection)
ax.set_xlim([-100,-70])
ax.set_ylim([35,65])
lon_formatter = LongitudeFormatter(zero_direction_label=True)
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
p = ax.pcolormesh(X, Y, data4,
                  vmin=-100000, #!important
                  vmax=100000,  #!important
                  transform=projection,
                  cmap='RdBu')
#!Plot the shared colorbar
axgr.cbar_axes[0].colorbar(p)
plt.tight_layout()
plt.show()
fig.savefig('CombinedMapWithSameColorbar.png',dpi=150)