height of colorbar (matplotlib)

Question:

Is it possible to have a colorbar smaller than the height of figure?
I know that we can adjust the size of colorbar with

divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="3%", pad=0.05)
pl.colorbar(im, cax=cax)

size only adjust the width of colorbar. but how about the height?

I am looking for a colorbar at upper right, with the half height of the figure.

Asked By: Abolfazl

||

Answers:

You can use fig.add_axes and fine-tune the parameters as you wish:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np

fig = plt.figure()
ax = plt.subplot(111)
im = ax.imshow(np.arange(100).reshape((10, 10)))

c = plt.colorbar(im, cax = fig.add_axes([0.78, 0.5, 0.03, 0.38]))

Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.