I am trying to set up a colorbar that automatically resizes if I zoom in to an image (which changes the aspect ratio of the axes, so I want the colorbar to get resized too). Let's say I have two Axes instances, say ax (for the main image) and cax (for the colorbar). I can set up a callback if the view limits in one axes change, for example
Now I can store a reference to cax inside ax:
ax._cax = cax
And I can now define update_colorbar so that it basically changes the position of cax:
# Get current position
xmin = ax..get_position().xmin
# Compute new colorbar position
# Set new position
# Return axes instance
Now the issue is that if I select a region of the image to zoom into, then as soon as I've selected the region, update_colorbar gets called, but by then, the aspect ratio of ax hasn't changed, and so the position I find when I do xmin = ax..get_position().xmin in update_colorbar is the *old* position of ax, not the new one. So the colorbar position is always one step behind compared to the main image axes.
Can anyone think of any way that would avoid this issue, and to be able to use the *new* position of ax inside update_colorbar?
Thanks in advance for any help,