我有 4 个 numpy 数组格式的图像,其中每个图像都是 4D(61、73、61、11),最后一个维度对应于图像通道(在我的例子中为 11)。我使用 for 循环迭代通道,并在每次迭代时为每个图像创建一个包含 4 个图的子图。在 jupyter 笔记本中,我可以看到所有子图,但我想创建一个包含所有子图的图形,这样我就可以创建一个 png 而不是 11。这是 matplotlib 中的代码。import maplotlib.pyplot as pltcenter_slices = [s//2 for s in concat_img.shape[:1]] # take the middle sliceprint(np.squeeze(concat_img[center_slices[0], :, :, 5]).shape)for i in range(10): f, axarr = plt.subplots(1, 4, figsize=(20,5), sharex=True); f.suptitle('Different intensity normalisation methods on brain fMRI image dual_regression + ALFF derivatives') img = axarr[0].imshow(np.squeeze(concat_img[:, :, center_slices[0], i]), cmap='gray'); axarr[0].axis('off') axarr[0].set_title('Original image') f.colorbar(img, ax=axarr[0]) img = axarr[1].imshow(np.squeeze(concat_img_white[:, :, center_slices[0], i]), cmap='gray'); axarr[1].axis('off') axarr[1].set_title('Zero mean/unit stdev') f.colorbar(img, ax=axarr[1]) img = axarr[2].imshow(np.squeeze(concat_img_zero_one[:, :, center_slices[0], i]), cmap='gray'); axarr[2].axis('off') axarr[2].set_title('[0,1] rescaling') f.colorbar(img, ax=axarr[2]) img = axarr[3].imshow(np.squeeze(concat_img_one_one[:, :, center_slices[0], i]), cmap='gray'); axarr[3].axis('off') axarr[3].set_title('[-1,1] rescaling') f.colorbar(img, ax=axarr[3]) f.subplots_adjust(wspace=0.05, hspace=0, top=0.8)# plt.savefig('./TTT.{0:07d}.png'.format(i)) # save each subplot in png plt.show();
1 回答
GCT1015
TA贡献1827条经验 获得超4个赞
这可以通过使用 创建一个坐标区实例来完成nrows != 1。我在下面附上了一个例子。
import matplotlib.pyplot as plt
import numpy as np
nrows = 5
ncols = 4
xdata = np.linspace(-np.pi, np.pi)
ydata = 1 * xdata
X, Y = np.meshgrid(xdata, ydata)
zdata = np.sin(X + Y)
fig, ax = plt.subplots(nrows=nrows, ncols=ncols, sharex=True,
figsize=(nrows * 2.2, 2 * ncols))
for j in range(nrows):
for i in range(ncols):
cbar = ax[j, i].contourf(zdata)
fig.colorbar(cbar, ax=ax[j, i])
fig.tight_layout()
添加回答
举报
0/150
提交
取消