You are reading an old version of the documentation (v1.5.0). For the latest version see

We're updating the default styles for Matplotlib 2.0

Learn what to expect in the new updates


Previous topic

pylab_examples example code:

Next topic

pylab_examples example code:

This Page

pylab_examples example code: image_slices_viewer.pyΒΆ

(Source code, png, hires.png, pdf)

from __future__ import print_function
import numpy
from matplotlib.pyplot import figure, show

class IndexTracker(object):
    def __init__(self, ax, X): = ax
        ax.set_title('use scroll wheel to navigate images')

        self.X = X
        rows, cols, self.slices = X.shape
        self.ind = self.slices/2 = ax.imshow(self.X[:, :, self.ind])

    def onscroll(self, event):
        print("%s %s" % (event.button, event.step))
        if event.button == 'up':
            self.ind = numpy.clip(self.ind + 1, 0, self.slices - 1)
            self.ind = numpy.clip(self.ind - 1, 0, self.slices - 1)


    def update(self):[:, :, self.ind])
        ax.set_ylabel('slice %s' % self.ind)

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)

Keywords: python, matplotlib, pylab, example, codex (see Search examples)