如何用matplotlib在二维图形上绘制数组形状(4,4,4,5,5)?

2024-06-28 12:00:29 发布

您现在位置:Python中文网/ 问答频道 /正文

matplotlib中使用gridspec.GridSpecFromSubplotSpec,我成功地将一个形状为(4,4,5,5)的数组分为4个子图(每个都是一个小图形,有4个宽度和高度为5的图像)到一个更大的2d图形上。在

我想知道如何将带有形状(4,4,4,5,5)(每个子图都是如上所述的图形)的数组绘制到一个更大的二维图形上?在

见下图:我设法绘制了1-3层,但我不知道如何绘制4层。有人能帮忙吗?谢谢 enter image description here


Tags: 图像图形宽度高度matplotlib绘制数组形状
1条回答
网友
1楼 · 发布于 2024-06-28 12:00:29

谢谢你让我用shape(4,4,5,5)来绘制数组,当我用更简单的数据集处理它时,我意识到我也可以绘制(4,4,4,5,5)。在

下面是绘制形状为(4,4,5,5)和(4,4,4,5,5)的数组的代码

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()

相关问题 更多 >