imshow() subplots generate unwanted white spaces(Imshow()子图生成不需要的空格)
问题描述
绘制两个(或多个)子图时,图中(所有四面)都有大片空白区域,如下所示:
以下是我用来绘制它的代码。
from pylab import *
from matplotlib import rc, rcParams
import matplotlib.pyplot as plt
for kk in range(57,58):
fn_i=str(kk)
image_file_1='RedshiftOutput00'+fn_i+'_Slice_z_RadioPowerDSA.png'
image_file_2='RedshiftOutput00'+fn_i+'_Slice_z_RadioPowerTRA.png'
image_file_3='RedshiftOutput00'+fn_i+'_Slice_z_RadioPowerDSA+TRA.png'
image_1 = plt.imread(image_file_1)
image_2 = plt.imread(image_file_2)
image_3 = plt.imread(image_file_3)
ax1 = subplot(131)
plt.imshow(image_1)
plt.axis('off') # clear x- and y-axes
ax2 = subplot(132)
plt.imshow(image_2)
plt.axis('off') # clear x- and y-axes
ax3 = subplot(133)
plt.imshow(image_3)
plt.axis('off') # clear x- and y-axes
plt.savefig('RedshiftOutput00'+fn_i+'_all.png')
我还上传了此代码中使用的3个图像,以使代码成为最小的工作示例
1)https://drive.google.com/file/d/0B6l5iRWTUbHWSTF2R3E1THBGeVk/view?usp=sharing
2)https://drive.google.com/file/d/0B6l5iRWTUbHWaFI4dHAzcWpiOEU/view?usp=sharing
3)https://drive.google.com/file/d/0B6l5iRWTUbHWaG8xclFlcGJNaUk/view?usp=sharing
如何删除此空格?我试着固定了整个地块的大小,仍然出现了空白。
推荐答案
MEL的上述注释(使用plt.tight_layout())在许多情况下都适用,但有时需要更多的控制。要更精细地操作轴(例如,当您有许多颜色条或twin轴时),可以使用plt.subplots_adjust()或GridSpec对象。
GridSpec对象允许您指定各个轴的水平和垂直范围,以及它们的比例宽度和高度和间距。subplots_adjust()在已在轴上绘制内容后移动轴。我更喜欢使用第一个选项,但这两个选项都是documentedwell。
它也可能有助于玩弄你的身材。如果您在宽度方向上有很多空格,请使图形的宽度更小。
以下是我用来设置最近绘图的一些示例代码:
gs = gridspec.GridSpec(
nrows=1, ncols=3, left=0.1, bottom=0.25, right=0.95, top=0.95,
wspace=0.05, hspace=0., width_ratios=[1, 1, 1])
NII_ax = plt.subplot(gs[0])
SII_ax = plt.subplot(gs[1])
OI_ax = plt.subplot(gs[2])
结果:
然后,如果您需要颜色条,请将GridSpec中的right参数调整为类似于0.85的值,并将fig.add_axes()与列表[left_lim, bottom, width, height]一起使用,并将其用作fig.colorbar()
这篇关于Imshow()子图生成不需要的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Imshow()子图生成不需要的空格
基础教程推荐
- Discord.py 缺少必需的参数 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
