I would like to plot a colorbar which automatically gets resized when I change the view limits and the aspect ratio of the main axes. So for example:
import matplotlib.pyplot as mpl
import numpy as np
fig = mpl.figure()
ax = fig.add_axes([0.1,0.1,0.7,0.8])
cax = fig.add_axes([0.81,0.1,0.02,0.8])
image = ax.imshow(np.random.random((100,100)))
Is fine, but then if I interactively select a sub-region to zoom in with a different aspect ratio, which I can also emulate by doing
The colorbar is then too high. If I then do
The height is fine but the position would need changing.
Is there an easy way to get around this issue and have the colorbar always at a fixed distance from the main axes, and also have it resize? Or is the only way to write this all explicitly using event callbacks?
Thanks for any help,