【发布时间】:2017-04-25 14:42:18
【问题描述】:
我正在做一个项目,我需要将一个 10 行和 3 列的绘图网格放在一起。虽然我已经能够制作情节并安排子情节,但我无法制作一个没有空白的漂亮情节,例如下面来自gridspec documentatation. 的这个。
我尝试了以下帖子,但仍然无法完全删除示例图像中的空白。有人可以给我一些指导吗?谢谢!
下面是我的代码。 The full script is here on GitHub。 注意:images_2 和 images_fool 都是形状为 (1032, 10) 的扁平图像的 numpy 数组,而 delta 是形状为 (28, 28) 的图像数组。
def plot_im(array=None, ind=0):
"""A function to plot the image given a images matrix, type of the matrix: \
either original or fool, and the order of images in the matrix"""
img_reshaped = array[ind, :].reshape((28, 28))
imgplot = plt.imshow(img_reshaped)
# Output as a grid of 10 rows and 3 cols with first column being original, second being
# delta and third column being adversaril
nrow = 10
ncol = 3
n = 0
from matplotlib import gridspec
fig = plt.figure(figsize=(30, 30))
gs = gridspec.GridSpec(nrow, ncol, width_ratios=[1, 1, 1])
for row in range(nrow):
for col in range(ncol):
plt.subplot(gs[n])
if col == 0:
#plt.subplot(nrow, ncol, n)
plot_im(array=images_2, ind=row)
elif col == 1:
#plt.subplot(nrow, ncol, n)
plt.imshow(w_delta)
else:
#plt.subplot(nrow, ncol, n)
plot_im(array=images_fool, ind=row)
n += 1
plt.tight_layout()
#plt.show()
plt.savefig('grid_figure.pdf')
【问题讨论】:
标签: python numpy matplotlib