图像切片查看器
滚动三维阵列的二维图像切片。

import numpy as npimport matplotlib.pyplot as pltclass IndexTracker(object):def __init__(self, ax, X):self.ax = axax.set_title('use scroll wheel to navigate images')self.X = Xrows, cols, self.slices = X.shapeself.ind = self.slices//2self.im = ax.imshow(self.X[:, :, self.ind])self.update()def onscroll(self, event):print("%s %s" % (event.button, event.step))if event.button == 'up':self.ind = (self.ind + 1) % self.sliceselse:self.ind = (self.ind - 1) % self.slicesself.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, ax = plt.subplots(1, 1)X = np.random.rand(20, 20, 40)tracker = IndexTracker(ax, X)fig.canvas.mpl_connect('scroll_event', tracker.onscroll)plt.show()
