Saturday, August 22, 2009

Creating a color bar using inset axes


An example to create an inset axes outside the axes area, and use it as a colorbar axes.



import matplotlib.pyplot as plt

from mpl_toolkits.axes_grid.inset_locator import inset_axes

fig = plt.figure(1, [5.5, 3])

# first subplot
ax = fig.add_subplot(111)
ax.set_aspect(1.)

axins = inset_axes(ax,
width="5%", # width = 10% of parent_bbox width
height="50%", # height : 50%
loc=3)

# Unfortunately, inset_axes currently ignores bbox_to_anchor and
# borderpad parameters, which is a bug.
# As a workaround, set these values after axes creation.

locator=axins.get_axes_locator()
locator.set_bbox_to_anchor((1.05, 0, 1, 1), ax.transAxes)
locator.borderpad = 0.
# Controlling the placement of the inset axes is basically same as that
# of the legend. you may want to play with the borderpad value and
# the bbox_to_anchor coordinate.

im=ax.imshow([[1,2],[2, 3]])
plt.colorbar(im, cax=axins)

plt.draw()
plt.show()

No comments:

Followers