Reputation: 1448
with gridspec.GridSpecFromSubplotSpec
in matplotlib
, I managed to plot an array with shape (4, 4, 5, 5) into 4 subplots (each is a small figure with 4 images with width and height being 5) onto a larger 2d figure.
I wonder how to plot array with shape (4, 4, 4, 5, 5) (each subplot is a figure like described above) onto a larger 2d figure?
see illustration below: I managed to plot level 1-3, but I don't know how to plot level4. Could anyone help? Thanks
Upvotes: 1
Views: 605
Reputation: 1448
Thanks for asking me to write codes for plotting array with shape (4,4,5,5), when I made it work with simpler dataset, I realize I can plot (4,4,4,5,5) too.
Below are codes to plot arrays with shape (4,4,5,5) and (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()
Upvotes: 1