In this recipe, we will learn how to use AxesDivider to plot the colorbar for an image, ensuring that it is properly aligned with the image. We have used pyplot.colorbar() many times, but at times, colorbar goes beyond the image boundaries. With AxesDivider and ImageGrid, we will not see this problem, as shall be shown in this recipe.
We will plot the image on three axes—one using a regular pyplot.colorbar() without AxesDivider; the second using AxesDivider and a vertical colorbar on the right-hand side; and the third again using AxesDivider, but this time with a horizontal colorbar on top of the image.