Python 使用matplotlib在重叠三维图像的二维切片中滚动

Python 使用matplotlib在重叠三维图像的二维切片中滚动,python,matplotlib,visualization,Python,Matplotlib,Visualization,我有一段代码,它可以很好地滚动3D numpy数组的2D切片 import matplotlib.pyplot as plt import numpy as np class IndexTracker(object): def __init__(self, ax, X): self.ax = ax ax.set_title('use scroll wheel to navigate images') self.X = X

我有一段代码,它可以很好地滚动3D numpy数组的2D切片

import matplotlib.pyplot as plt
import numpy as np


class IndexTracker(object):
    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], cmap="gray")
        self.update()

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

    def update(self):
        self.im.set_data(self.X[:, :, self.ind])
        self.ax.set_ylabel('slice %s' % self.ind)
        self.im.axes.figure.canvas.draw()


def plot3d(image):
    fig, ax = plt.subplots(1, 1)
    tracker = IndexTracker(ax, image)
    fig.canvas.mpl_connect('scroll_event', tracker.onscroll)
    plt.show()


if __name__ == "__main__":
    img = np.array([[[0, 0, 0], [0, 1, 0], [0, 0, 0]],
                     [[0, 0, 0], [1, 1, 1], [0, 0, 0]],
                     [[0, 0, 0], [0, 1, 0], [0, 0, 0]]])

    plot3d(img)
我希望有相同的功能,但滚动通过两个同样大小的3D numpy阵列在同一时间。其中一个阵列应显示一定程度的不透明度和不同的颜色方案,以便可以同时检查两个阵列。对于2D切片,无需滚动即可轻松实现:

img1 = np.array([[[0, 0, 0], [0, 1, 0], [0, 0, 0]],
                 [[0, 0, 0], [1, 1, 1], [0, 0, 0]],
                 [[0, 0, 0], [0, 1, 0], [0, 0, 0]]])

img2 = np.array([[[0, 0, 0], [0, 1, 0], [0, 0, 0]],
                 [[0, 1, 0], [0, 1, 0], [0, 1, 0]],
                 [[0, 0, 0], [0, 1, 0], [0, 0, 0]]])

plt.imshow(img1[:, :, 1], cmap="gray")
plt.imshow(img2[:, :, 1], cmap="jet", alpha=0.25)
plt.show()
我尝试扩展IndexTracker类以接受第二个3D数组,并使用imshow()显示每个卷的一个切片(具有相同的索引)。此外,还打算使用set_data()更新每个滚动事件上显示的图像。然而,这并没有成功

import numpy as np
import matplotlib.pyplot as plt


class IndexTracker(object):
    def __init__(self, ax, X, Y):
        self.ax = ax
        self.X = X
        self.Y = Y
        _, _, self.slices = X.shape
        self.ind = self.slices // 2

        self.im = ax.imshow(self.X[:, :, self.ind], cmap="gray")
        self.im = ax.imshow(self.Y[:, :, self.ind], cmap="jet", alpha=0.25)

        self.update()

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

    def update(self):
        self.im.set_data(self.X[:, :, self.ind])
        self.im.set_data(self.Y[:, :, self.ind])

        self.ax.set_ylabel('slice %s' % self.ind)
        self.im.axes.figure.canvas.draw()


def plot3d(image1, image2):
    image1 = np.rot90(image1, k=-1)
    image2 = np.rot90(image2, k=-1)
    fig, ax = plt.subplots(1, 1)
    tracker = IndexTracker(ax, image1, image2)
    fig.canvas.mpl_connect('scroll_event', tracker.onscroll)
    plt.show()


if __name__ == "__main__":
    img1 = np.array([[[0, 0, 0], [0, 1, 0], [0, 0, 0]],
                     [[0, 0, 0], [1, 1, 1], [0, 0, 0]],
                     [[0, 0, 0], [0, 1, 0], [0, 0, 0]]])
    img2 = np.array([[[0, 0, 0], [0, 1, 0], [0, 0, 0]],
                     [[0, 1, 0], [0, 1, 0], [0, 1, 0]],
                     [[0, 0, 0], [0, 1, 0], [0, 0, 0]]])

    plot3d(img1, img2)
对于如何使用matplotlib解决给定的问题,您有什么想法吗?理想情况下,扩展包含IndexTracker类的第一个代码段

编辑:
如果您分别跟踪两个
轴.imshow
对象(由
plt.imshow
返回),那么matplotlib将为您处理图像的分层,这将非常高兴地将第二个图像作为参数添加到plot3d()调用中。然后,您可以分别对每个对象使用set_数据。执行此操作时,您需要为每个图像保留相同的颜色贴图和alpha值,您可以使用
im.to_rgba
im.get_alpha
的组合来完成此操作。以下是您需要为这项工作对类进行的修改:

class IndexTracker(object):
    def __init__(self, ax, X, Y):
        ...

        self.im1 = ax.imshow(self.X[:, :, self.ind], cmap="gray")
        self.im2 = ax.imshow(self.Y[:, :, self.ind], cmap="jet", alpha=.25)


        ...


    def update(self):
        im1_data = self.im1.to_rgba(self.X[:, :, self.ind], alpha=self.im1.get_alpha())
        im2_data = self.im2.to_rgba(self.Y[:, :, self.ind], alpha=self.im2.get_alpha())

        self.im1.set_data(im1_data)
        self.im2.set_data(im2_data)

        ...

plot3d
需要拍摄两幅图像,而不是一幅。谢谢您的回答!不幸的是,这只是我在创建示例代码时犯的一个错误。即使将两个三维阵列用作
plot3d
调用的参数,我也无法同时滚动两个图像。这里更具体一点:如果我使用上面的代码,我会滚动其中一个图像,而另一个图像仍停留在最初显示的2D幻灯片(中间幻灯片)上。显然,您不能用相同的名称调用两个图像
self.im