python - 在 Matplotlib 中定义 GridSpec 的宽度/高度
问题描述
我有一个简单的情节来比较一些图像,如下所示:
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 表格。
有什么想法可以改进上面的代码吗?
解决方案
无需为颜色条制作网格规范:
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')
推荐阅读
- html - 如何防止我网站中的视频在 ipad 上以 PIP 模式播放?
- c++ - 是否可以 dllexport 已通过 using-declaration 访问的方法?
- java - 如何验证是否显示 toast 消息?
- mongodb - Mongodb聚合数组分区和计数
- java - Wildfly 23/24 (Jakarta JWS) 中禁用了 WebServices 子系统
- google-cloud-platform - 如果不是为了模拟,“服务帐户用户”角色的意义何在?
- input - 为什么输入(colorPicker)只支持Hex?
- python - 尝试复制已经工作的代码,但在导入过程中遇到错误
- amazon-web-services - EKS aws-ebs-csi-driver 无法挂载卷
- dart - Dart Mixin 获取超字段值