I'm trying to make a subplot with three plots next to each other, and then a colorbar on the right side of the last plot (see figure).
I'm doing it with this code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams
from mpl_toolkits.axes_grid1 import make_axes_locatable
x = np.linspace(1, 100, 100)
y = np.linspace(0.1, 10, 100)
z = x[:, np.newaxis] + y[np.newaxis, :]
fig, ax = plt.subplots(1, 3, figsize=(12, 4))
ax[0].contourf(x, y, z)
ax[0].set_xlabel('x')
ax[0].set_ylabel('y')
ax[1].contourf(x, y, z)
ax[1].set_xlabel('x')
ax[1].set_ylabel('y')
plt.contourf(x, y, z)
ax[2].set_xlabel('x')
ax[2].set_ylabel('y')
divider = make_axes_locatable(plt.gca())
cax = divider.append_axes("right", "10%", pad="3%")
plt.colorbar(cax=cax)
plt.tight_layout()
plt.show()
My problem is that 1) I don't think the first two plots are completely square (which I would like them to be), 2) the last plot that includes the colorbar is smaller in width than the two others. Is there some easy trick to fix this, or do I manually have to go in and give one a little more padding than the other an so on.