Hi,
I have a problem placing the colorbar next to an imshow when I specify an aspect ratio. Below is a minimal example showing my problem where the colorbar in the first plot looks good while in the second there is far too much whitespace between the figure and the colorbar. I have looked at Overview of axes_grid1 toolkit — Matplotlib 3.3.4 documentation but can’t seem to figure out how to make it look good.
Any help appreciated,
Erik
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.axes_grid1 import make_axes_locatable
img_file = ‘https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/Cat-relaxed.jpg/640px-Cat-relaxed.jpg’
img = plt.imread(img_file, format=‘jpg’)
cmap = cm.Blues
plt.figure(10, clear=True)
ax = plt.subplot()
im = ax.imshow(img, aspect=1)
divider = make_axes_locatable(ax)
cax = divider.append_axes(“right”, size=“5%”, pad=0.05)
plt.colorbar(cm.ScalarMappable(cmap=cmap), cax=cax)
plt.figure(20, clear=True)
ax = plt.subplot()
im = ax.imshow(img, aspect=3)
divider = make_axes_locatable(ax)
cax = divider.append_axes(“right”, size=“5%”, pad=0.05)
plt.colorbar(cm.ScalarMappable(cmap=cmap), cax=cax)
plt.show()