Maintain the subplot size when colorbar is shown
It is important to add colorbar to your figures to show the duynamic range of the image colormap.
import numpy as np
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 3,figsize=(12,4))
vmin = -2
vmax = 2
for ax in axes:
im=np.random.normal(size=[100,100])
a=ax.imshow(im, vmin=vmin, vmax=vmax, cmap='Blues')
cbar = fig.colorbar(a, ax=axes[-1], ticks=np.linspace(vmin,vmax,5))
This issue is fixed when you use the inset_locator
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import inset_locator
fig, axes = plt.subplots(1, 3,figsize=(12,4))
vmin = -2
vmax = 2
for ax in axes:
im=np.random.normal(size=[100,100])
a=ax.imshow(im, vmin=vmin, vmax=vmax, cmap='Blues')
axins = inset_locator.inset_axes(
axes[-1],
width="7.5%", # width: 7.5% of parent_bbox width
height="100%", # height: 100%
loc="lower left",
bbox_to_anchor=(1.05, 0., 1, 1),
bbox_transform=axes[-1].transAxes,
borderpad=0,
)
cbar = fig.colorbar(a, cax=axins, ticks=np.linspace(vmin,vmax,5))
cbar.ax.tick_params(labelsize=8)