Create a gist now

Instantly share code, notes, and snippets.

matplotlib imshow with auto-zoom on the color limits
import numpy as np
import matplotlib.pyplot as plt
def iround(x, x0):
return (np.abs(x-x0)).argmin()
class az_imshow:
def __init__(self, data, ax=None, **kwarg):
if ax == None:
ax = plt.gca() = data = ax
self.image = ax.imshow(data, **kwarg)
cid = ax.figure.canvas.mpl_connect('draw_event', self.redraw)
cid = ax.figure.canvas.mpl_connect('resize_event', self.redraw)
self.lims = np.r_[,]
def redraw(self, evt):
lims = np.r_[,]
print lims
if (self.lims == lims).all():
self.lims = lims
xmin, xmax, ymin, ymax = self.image.get_extent()
x = np.linspace(xmin, xmax,[1])
y = np.linspace(ymin, ymax,[0])
xind = [iround(x, ii) for ii in]
yind = [iround(y, ii) for ii in]
local_data =[yind[0]:yind[1],
self.image.set_clim(local_data.min(), local_data.max())
if __name__ == "__main__":
fig = plt.figure()
ax1 = fig.add_subplot(211)
az_imshow(data, ax1, interpolation='nearest', aspect='auto')
az_imshow(data, aspect='auto')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment