import matplotlib.pyplot as plt

from mpl_toolkits.axes_grid1.inset_locator import inset_axes

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

# first subplot
ax1 = fig.add_subplot(111)

axins1 = inset_axes(ax1,
                    width="50%", # width = 10% of parent_bbox width
                    height="5%", # height : 50%
                    loc=1)

im1=ax1.imshow([[1,2],[2, 3]])
plt.colorbar(im1, cax=axins1, orientation="horizontal", ticks=[1,2,3])
axins1.xaxis.set_ticks_position("bottom")


from matplotlib.patches import Patch
from matplotlib.path import Path
from matplotlib.transforms import IdentityTransform

class TightBboxPatch(Patch):
    def __init__(self, ax, **kwargs):
        kwargs["transform"] = IdentityTransform()
        Patch.__init__(self, **kwargs)
        self._ax = ax
        self._renderer = None

    def draw(self, renderer):
        self._renderer = renderer
        Patch.draw(self, renderer)

    def get_path(self):
       if self._renderer == None:
            x0, y0, x1, y1 = 0, 0, 0, 0
       else:
           bbox = self._ax.get_tightbbox(self._renderer)
           x0, y0, x1, y1 = bbox.padded(3).extents

       verts = [(x0, y0),
                (x1, y0),
                (x1, y1),
                (x0, y1),
                (x0, y0),
                (0,0)]

       codes = [Path.MOVETO,
                Path.LINETO,
                Path.LINETO,
                Path.LINETO,
                Path.LINETO,
                Path.CLOSEPOLY]

       return Path(verts, codes)



p_bbox = TightBboxPatch(axins1, fc="w", ec="none")

ax1.add_patch(p_bbox)


plt.show()
