 # Rescale colors of imshow after zoom?

While I though this would be fairly
easy, I’ve yet to find a decent solution. I’m interested in using the
RangeSelector or standard zoom feature along with an autoscaling
function. In the example below there are two gaussian profiles created
in a matrix. I would like to have the behavior in which when I zoom in
that the Z scale is reordered to account for the new range of data. Is
my only solution to take the limits returned by a RangeSelector event
and extract a sub-matix from the original data and replot each time I
zoom in or out or is there a more elegant solution? Is there a way to also extend such functionality to contour, pcolor, etc.?

Cheers,

Brian

######paste in ipython###########################################

from numpy import *

from matplotlib.widgets import RectangleSelector

import pylab as P

def gaussian(height, center_x, center_y, width_x, width_y):

``````"""Returns a gaussian function with the given parameters"""

width_x = float(width_x)

width_y = float(width_y)

return lambda x,y: height*exp(-(((center_x-x)/width_x)**2+((center_y-y)/width_y)**2)/2)
``````

def line_select_callback(event1, event2):

``````'event1 and event2 are the press and release events'

x1, y1 = event1.xdata, event1.ydata

x2, y2 = event2.xdata, event2.ydata

print "(%3.2f, %3.2f) --> (%3.2f, %3.2f)"%(x1,y1,x2,y2)

print " The button you used were: ",event1.button, event2.button
``````

cla()

# Create the gaussian data

Xin, Yin = mgrid[0:201, 0:201]

Xin2, Yin2 = mgrid[0:201, 0:201]

data = gaussian(20, 100, 100, 20, 40)(Xin, Yin) + random.random(Xin.shape)

data2 = gaussian(10, 10, 10, 2, 4)(Xin2, Yin2) + random.random(Xin.shape)

P.imshow(data+data2, cmap=cm.jet)

current_ax = gca()

LS = RectangleSelector(current_ax, line_select_callback, drawtype=‘box’,useblit=True,\

``````                   minspanx=5,minspany=5,spancoords='pixels')
``````

P.show()