You are reading documentation for the unreleased version of Matplotlib. Try searching for the released version of this page instead?
Applications are open for the 2018 John Hunter Matplotlib Summer Fellowship. Apply now!
Version 2.2.2.post1772+gd9240e24b
Fork me on GitHub

Related Topics

Image Slices ViewerΒΆ

Scroll through 2D image slices of a 3D array.

import numpy as np
import matplotlib.pyplot as plt

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 = (self.ind + 1) % self.slices
            self.ind = (self.ind - 1) % self.slices

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

fig, ax = plt.subplots(1, 1)

X = np.random.rand(20, 20, 40)

tracker = IndexTracker(ax, X)

fig.canvas.mpl_connect('scroll_event', tracker.onscroll)

Keywords: matplotlib code example, codex, python plot, pyplot Gallery generated by Sphinx-Gallery