Python 如何使用matplotlib将阵列形状(4、4、4、5、5)绘制到二维图形上?

Python 如何使用matplotlib将阵列形状(4、4、4、5、5)绘制到二维图形上?,python,matplotlib,Python,Matplotlib,使用matplotlib中的gridspec.gridspecfromsublotspec,我成功地将一个具有形状(4,4,5,5)的数组绘制成4个子图(每个子图是一个小图形,其中4个图像的宽度和高度为5)到一个较大的二维图形上 我想知道如何将具有形状(4,4,4,5,5)(每个子图都是如上所述的图形)的数组绘制到更大的2d图形上 请参见下图:我设法绘制了标高1-3,但不知道如何绘制标高4。有人能帮忙吗?谢谢 谢谢你让我写代码来绘制形状为(4,4,5,5)的数组,当我使用更简单的数据集时,我意识

使用
matplotlib
中的
gridspec.gridspecfromsublotspec
,我成功地将一个具有形状(4,4,5,5)的数组绘制成4个子图(每个子图是一个小图形,其中4个图像的宽度和高度为5)到一个较大的二维图形上

我想知道如何将具有形状(4,4,4,5,5)(每个子图都是如上所述的图形)的数组绘制到更大的2d图形上

请参见下图:我设法绘制了标高1-3,但不知道如何绘制标高4。有人能帮忙吗?谢谢
谢谢你让我写代码来绘制形状为(4,4,5,5)的数组,当我使用更简单的数据集时,我意识到我也可以绘制(4,4,4,5,5)

下面是用形状(4,4,5,5)和(4,4,4,5,5)绘制阵列的代码


向你的代码展示你成功地绘制了1-3级曲线,当我写完3级曲线时,我意识到这个问题也得到了回答。
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import math
import numpy as np

#### plot (4,4,5,5)
data_4d = np.linspace(0, 1, 400).reshape((4,4,5,5))
num_out_img, num_inner_img, img_w, img_h = data_4d.shape

fig = plt.figure(1, figsize=(6, 6))

outer_grid = math.ceil(math.sqrt(num_out_img))
inner_grid = math.ceil(math.sqrt(num_inner_img))

outer_frame = gridspec.GridSpec(outer_grid, outer_grid)

for sub in range(num_out_img):

    inner_frame = gridspec.GridSpecFromSubplotSpec(inner_grid, inner_grid, subplot_spec=outer_frame[sub], wspace=0.0, hspace=0.0)

    for sub_sub in range(num_inner_img):

        ax = plt.Subplot(fig, inner_frame[sub_sub])
        ax.imshow(data_4d[sub, sub_sub, :, :], cmap='gray')
        fig.add_subplot(ax)

plt.show()

#### plot (4,4,4,5,5)
data_5d = np.linspace(0, 1, 1600).reshape((4,4,4,5,5))
num_out_img, num_inner_img, num_deep_img, img_w, img_h = data_5d.shape

fig = plt.figure(1, figsize=(6, 6))

outer_grid = math.ceil(math.sqrt(num_out_img))
inner_grid = math.ceil(math.sqrt(num_inner_img))
deep_grid = math.ceil(math.sqrt(num_deep_img))

outer_frame = gridspec.GridSpec(outer_grid, outer_grid)

for sub in range(num_out_img):

    inner_frame = gridspec.GridSpecFromSubplotSpec(inner_grid, inner_grid, subplot_spec=outer_frame[sub], wspace=0.0, hspace=0.0)

    for sub_sub in range(num_inner_img):

        deep_frame = gridspec.GridSpecFromSubplotSpec(deep_grid, deep_grid, subplot_spec=inner_frame[sub_sub], wspace=0.0, hspace=0.0)

        for deep in range(num_deep_img):

            ax = plt.Subplot(fig, deep_frame[deep])
            ax.imshow(data_5d[sub, sub_sub, deep, :, :], cmap='gray')
            fig.add_subplot(ax)

plt.show()