I generated three plots using:
fig, ax = plt.subplots(1,3,figsize=(12,4))
X,Y=np.meshgrid(lon,lat)
P1=ax[0].contourf(X,Y,A)
ax[0].set_title('Plot A')
P2=ax[1].contourf(X,Y,B)
ax[1].set_title('Plot B')
P3=ax[2].contourf(X,Y,C)
ax[2].set_title('Plot C')
How can I add one colorbar for all subplots?
You may want to look at Matplotlib's ImageGrid:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
from matplotlib.colors import Normalize
from matplotlib.cm import ScalarMappable
x, y = np.mgrid[-2:2:100j, -2:2:100j]
z1 = np.sin(x**2 + y**2)
z2 = np.cos(x**2 + y**2)
cmap = "inferno"
fig = plt.figure()
grid = ImageGrid(
fig, 111, # similar to subplot(111)
nrows_ncols=(1, 2), # creates 1x2 grid of Axes
axes_pad=0.2, # pad between Axes in inch.
cbar_mode="single"
)
grid[0].contourf(x, y, z1, cmap=cmap)
grid[1].contourf(x, y, z2, cmap=cmap)
norm = Normalize(vmin=min(z1.min(), z2.min()), vmax=max(z1.max(), z2.max()))
cb2 = grid[1].cax.colorbar(
ScalarMappable(norm=norm, cmap=cmap),
label = "z value"
)
plt.show()