0

I have subplots that extend across 8 rows and 1 column and I would like to have one shared colorbar at the bottom of the last subplot. However, when I attempt this, the size of the last subplot gets changed, leaving it displaced from the 7 plots above it.

If I use these colorbar settings for an individual plot, everything looks fine. But attempting to use it for the 8 subplots leaves me with the sizing problem. I've truncated the code below for the sake of space, but this shows what I have for the last 2 subplots.

from netCDF4 import Dataset as NetCDFFile
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
from mpl_toolkits.basemap import addcyclic

fig = plt.figure()

ax = fig.add_subplot(817)

nc = NetCDFFile('C:/Reanalysis Plots/netCDF files/phase7_olr_anom.nc')
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
time = nc.variables['time'][:]
olr = nc.variables['olr'][:]
olr,lon = addcyclic(olr,lon)
map = Basemap(llcrnrlon=0.,llcrnrlat=-40.,urcrnrlon=360.,urcrnrlat=40.,resolution='l')
lons,lats = np.meshgrid(lon,lat)
x,y = map(lons,lats)

levels = np.arange(-22.5,23.0,0.5)
levels = levels[levels!=0]
ticks = np.arange(-24.0,24.0,4.0)
plt.rcParams.update({'font.size': 5})
cs = map.contourf(x,y,olr[0],levels, cmap='bwr')
#cbar = plt.colorbar(cs, orientation='horizontal', cmap='bwr', spacing='proportional', ticks=ticks)
#cbar.set_label('Outgoing Longwave Radiation Anomalies $\mathregular{(W/m^2)}$')
map.drawcoastlines(linewidth=0.5)
yticks = map.drawparallels(np.arange(-20,21,20),labels=[1,0,0,0], linewidth=0.5, fontsize=4)
xticks = map.drawmeridians(np.arange(0,361,40),labels=[0,0,0,0], linewidth=0.5, fontsize=4)

plt.annotate('7', xy=(0, 1), xytext=(138, -25), fontsize=6,
    xycoords='axes fraction', textcoords='offset points',
    bbox=dict(facecolor='white', alpha=0.9, pad=1.5),
    horizontalalignment='center', verticalalignment='center')



ax = fig.add_subplot(818)


nc = NetCDFFile('C:/Reanalysis Plots/netCDF files/phase8_olr_anom.nc')
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
time = nc.variables['time'][:]
olr = nc.variables['olr'][:]
olr,lon = addcyclic(olr,lon)
map = Basemap(llcrnrlon=0.,llcrnrlat=-40.,urcrnrlon=360.,urcrnrlat=40.,resolution='l')
lons,lats = np.meshgrid(lon,lat)
x,y = map(lons,lats)

levels = np.arange(-22.5,23.0,0.5)
levels = levels[levels!=0]
ticks = np.arange(-24.0,24.0,4.0)
plt.rcParams.update({'font.size': 5})
cs = map.contourf(x,y,olr[0],levels, cmap='bwr')
cbar = plt.colorbar(cs, orientation='horizontal', cmap='bwr', spacing='proportional', ticks=ticks)
#cbar.set_label('Outgoing Longwave Radiation Anomalies $\mathregular{(W/m^2)}$')
map.drawcoastlines(linewidth=0.5)
yticks = map.drawparallels(np.arange(-20,21,20),labels=[1,0,0,0], linewidth=0.5, fontsize=4)
xticks = map.drawmeridians(np.arange(40,360,40),labels=[0,0,0,1], linewidth=0.5, fontsize=4)

plt.annotate('8', xy=(0, 1), xytext=(138, -25), fontsize=6,
    xycoords='axes fraction', textcoords='offset points',
    bbox=dict(facecolor='white', alpha=0.9, pad=1.5),
    horizontalalignment='center', verticalalignment='center')



plt.subplots_adjust(hspace=0.04)
plt.savefig('olr_multipanel.png',dpi=600)

OLR Plot

bayouwxman
  • 79
  • 1
  • 3
  • 11

1 Answers1

2

Matplotlib 2 Subplots, 1 Colorbar shows various options on how to create one colorbar for multiple subplots. Here using a gridspec with an additional axes for the colorbar seems useful.

import matplotlib.pyplot as plt


fig, axes = plt.subplots(nrows=9, figsize=(6,9), 
                         gridspec_kw=dict(height_ratios=[1]*8+[0.2]))

fig.colorbar(plt.cm.ScalarMappable(), cax=axes[-1], orientation="horizontal")

plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712