From: John H. <jd...@gm...> - 2007-06-27 13:38:05
|
On 6/26/07, Jason Addison <jra...@gm...> wrote: > I would like to view a 3D array (or a python list of 2D arrays) as a > sequence of images using something like imshow. For example, something > like this: > > imshow(rand(6,12,12), imagecube=true) Here is a simple example showing how to do this -- we could add support for this in a built-in function. It would be nice if we encapsulated the scroll mouse in our event handling, since the scroll is the natural way to browse these images from pylab import figure, show class IndexTracker: def __init__(self, ax, X): self.ax = ax ax.set_title('use left mouse to advance, right to retreat') self.X = X rows,cols,self.slices = X.shape self.ind = self.slices/2 self.im = ax.imshow(self.X[:,:,self.ind]) self.update() def onpress(self, event): if event.button==1: self.ind = numpy.clip(self.ind+1, 0, self.slices-1) elif event.button==3: self.ind = numpy.clip(self.ind-1, 0, self.slices-1) print event.button, self.ind self.update() def update(self): self.im.set_data(self.X[:,:,self.ind]) ax.set_ylabel('slice %s'%self.ind) self.im.axes.figure.canvas.draw() fig = figure() ax = fig.add_subplot(111) X = numpy.random.rand(20,20,40) tracker = IndexTracker(ax, X) fig.canvas.mpl_connect('button_press_event', tracker.onpress) show() |