from matplotlib import pyplot as plt
import matplotlib as mpl
from mpl_toolkits.axes_grid import make_axes_locatable
import numpy as np

fig = plt.figure()
ax_im = fig.add_subplot(1, 1, 1)
divider = make_axes_locatable(ax_im)
ax_cb = divider.new_vertical(size = '20%', pad = 0.2, pack_start = True)
fig.add_axes(ax_cb)

x = np.linspace(-5, 5, 101)
y = x
Z = np.sin(x*y[:,None]).clip(-1,1-0.1)

# Leave out if you want:
Z += 2

min_val = Z.min()
max_val = Z.max()
bound = max(np.abs(Z.max()), np.abs(Z.min()))

patch = ax_im.imshow(Z, origin = 'upper', interpolation = 'nearest', 
		vmin = -bound, vmax = bound)

cb = fig.colorbar(patch, cax = ax_cb, orientation = 'horizontal', 
		norm = patch.norm, 
		boundaries = np.linspace(-bound, bound, 256),
		ticks = [min_val, 0, max_val],
		format = '%.2f')

plt.show()
