matplotlib/matplotlib — pull request #3957 by inclement (2014-12-30 01:25:56+00:00)
Title: Corrected cax attirbutes of ImageGrid axes

Following the documentation, each axis of an ImageGrid should have a `cax` attribute pointing to the axis to be used for its colorbar. However, when the axes of a row or column share a colorbar, this attribute is not set correctly - colorbar axes are assigned as if every axis has its own colorbar, so most of the `cax` attributes are not visible axes, and the axes do not match the correct row or column.

This PR changes the behaviour to correctly associate axes of a row/column with the colorbar at the end of that row/column.

Example code demonsrating the problem is:

from mpl_toolkits.axes_grid1 import ImageGrid
import matplotlib.pyplot as plt
import numpy as n

X, Y = n.meshgrid(n.linspace(0, 6, 30), n.linspace(0, 6, 30))
arr = n.sin(X) * n.cos(Y) + 1j*(n.sin(3*Y) * n.cos(Y/2.))

fig = plt.figure(figsize=(6, 6))

grid = ImageGrid(fig, 111,
nrows_ncols = (2, 2),
direction='row',
cbar_location='right',
cbar_mode='edge',
)
ax1, ax2, ax3, ax4, = grid

im1 = ax1.imshow(arr.real, cmap='spectral')
im2 = ax2.imshow(arr.imag, cmap='hot')
im3 = ax3.imshow(n.abs(arr), cmap='jet')
im4 = ax4.imshow(n.arctan2(arr.imag, arr.real), cmap='hsv')

ax2.cax.colorbar(im2)
ax4.cax.colorbar(im4)

fig.savefig('colorbar_failure.png')

Before this patch, the top-right colorbar will be empty while the bottom-right colorbar will show colours from one of the top-row plots. After this patch, the colorbars updated are those in the correct row/column to match the axis whose `cax` attribute was accessed.