I am trying to make a plot in which the color scale is updated on zoom on the base of the visualized data, using a scheme similar to e.g. http://matplotlib.org/examples/event_handling/viewlims.html (also note similar question Matplotlib imshow, dynamically resample based on zoom)
However I met a problem when dealing with the colorbar, after removing it and adding a new one, the zoom history is reset. In my real code, the colorbar update is done at every zoom, as a result back and home buttons in matplotlib plot don't work at all.
This is probably clearer looking at the example code below. What is the reason? Is there a way to prevent this from happening?
The code, stripped from all unnecessary parts looks more or less like this:
#create and plot a test image with colorbar,
#zoom and everything works
import numpy as np
N=100
a=np.random.random((N,N))
plt.figure()
plt.imshow(a,interpolation='none')
plt.colorbar()
plt.show()
#at this point I can zoom and use back and forward buttons as always
#but if I zoom in and then execute the following code, the history is reset and I cannot go back or home any more (the zooming history works in the future, but is reset every time I replace the colorbar):
ax=plt.gca()
im=ax.images[-1]
im.colorbar.remove()
#in real code, color range is updated here
plt.colorbar()