我实际上编写了代码来完成我认为您正在寻找的事情,看看这是否有帮助:
import numpy as np
import pylab
class plotter:
def __init__(self, im, i=0):
self.im = im
self.i = i
self.vmin = im.min()
self.vmax = im.max()
self.fig = pylab.figure()
pylab.gray()
self.ax = self.fig.add_subplot(111)
self.draw()
self.fig.canvas.mpl_connect('key_press_event',self)
def draw(self):
if self.im.ndim is 2:
im = self.im
if self.im.ndim is 3:
im = self.im[...,self.i]
self.ax.set_title('image {0}'.format(self.i))
pylab.show()
self.ax.imshow(im, vmin=self.vmin, vmax=self.vmax, interpolation=None)
def __call__(self, event):
old_i = self.i
if event.key=='right':
self.i = min(self.im.shape[2]-1, self.i+1)
elif event.key == 'left':
self.i = max(0, self.i-1)
if old_i != self.i:
self.draw()
self.fig.canvas.draw()
def slice_show(im, i=0):
plotter(im, i)
只需在您的 3d 数组上调用 show 函数,我会告诉它要显示的切片。只要您选择了绘图,就可以使用箭头键逐步浏览切片。
注意,这需要形状为 (x, y, z) 的数组,例如,您可以使用 np.dstack((im1, im2, ...)) 从一系列二维数组中获取这样的数组。
另请参阅Interactive matplotlib plot with two sliders 以获取使用 gui 滑块执行此操作的代码示例