我有一个简单的图来比较一些图像,如下所示:
fig = plt.figure(constrained_layout=True,figsize=(15,15))
gs = GridSpec(3,2, figure=fig)
axes = []
ax = fig.add_subplot(gs[0, 0])
ax.imshow(square, cmap='gray')
ax.set_title('Original')
axes.append(ax)
ax = fig.add_subplot(gs[0, 1])
im1 = ax.imshow(scaled_square_sobel, cmap='gray')
ax.set_title('Sobel')
axes.append(ax)
ax = fig.add_subplot(gs[1, 0])
im2 = ax.imshow(scaled_square_sobel_x, cmap='gray')
ax.set_title('Sobel X')
axes.append(ax)
ax = fig.add_subplot(gs[1, 1])
ax.imshow(scaled_square_sobel_y, cmap='gray')
ax.set_title('Sobel Y')
axes.append(ax)
ax = fig.add_subplot(gs[2,0:2])
fig.colorbar(im2, cax=ax, orientation='horizontal')
plt.suptitle('Comparacao Sobel');
看起来像这样
如您所见,颜色条占据了整个图的高度,而不是漂亮而优雅的小高度。如何“强制” GridSpec具有预定的高度。
我当然可以创建一个(30,20)的网格规格,并将每个图定义为更大的切片,但是恕我直言,这也不是很优雅,并且似乎我正在使用HTML表格。
有什么想法可以改进上面的代码吗?
答案 0 :(得分:1)
无需为颜色栏创建gridspec:
fig, axs = plt.subplots(constrained_layout=True,figsize=(15,15))
# other axes...
ax = axs[1, 1]
im2 = ax.imshow(np.random.rand(10,10), cmap='gray')
ax.set_title('Sobel Y')
fig.colorbar(im2, ax=axs, orientation='horizontal')