I’m looking for a way to more freely position colorbar. After review many online posts, I think end up using
import matplotlib.pyplot as plt import numpy as np np.random.seed(1) fig, ax = plt.subplots(figsize=(5, 5)) # inset axes to contain colorbar axins = ax.inset_axes([0.2, 0.8, 0.5, 0.6], frameon=True) im = ax.imshow(np.random.rand(11,16)) # plot colorbar cbar = fig.colorbar(im, ax=axins, orientation='horizontal', aspect=25) cbar.ax.set_title('colorbar')
which gives me:
However, if I change
ax.inset_axes([0.2, 0.2, 0.5, 0.6], frameon=True), I will get
The position in my inset axes changed to the bottom somehow. Meanwhile, the size of the colorbar could also be affected sometimes if you change the width and height of the inset axes.
I have also tried to change
panchor arg, but it doesn’t seem to do anything at all.
So I really wonder if there is a better way to control the position and size of the colorabr.