.. _pylab_examples-image_slices_viewer:

pylab_examples example code: image_slices_viewer.py
===================================================



.. plot:: /media/TOSHI/temp/numpy_scipy_matplotlib/matplotlib/matplotlib-1.1.1~rc2/doc/mpl_examples/pylab_examples/image_slices_viewer.py

::

    import numpy
    from matplotlib.pyplot import figure, show
    
    
    
    
    class IndexTracker:
        def __init__(self, ax, X):
            self.ax = ax
            ax.set_title('use scroll wheel to navigate images')
    
            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 onscroll(self, event):
            print event.button, event.step
            if event.button=='up':
                self.ind = numpy.clip(self.ind+1, 0, self.slices-1)
            else:
                self.ind = numpy.clip(self.ind-1, 0, self.slices-1)
    
    
            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('scroll_event', tracker.onscroll)
    show()
    

Keywords: python, matplotlib, pylab, example, codex (see :ref:`how-to-search-examples`)