Python imshow()子地块生成不需要的空白
当绘制两个(或多个)子图时,图内(所有四个面上)有大量空白区域,如下所示: 下面是我用来绘制它的代码Python imshow()子地块生成不需要的空白,python,python-2.7,matplotlib,whitespace,imshow,Python,Python 2.7,Matplotlib,Whitespace,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
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个图像,以使代码成为一个最小的工作示例
(一)
(二)
(三)
我们如何删除这个空白?我试图调整整个情节的大小,但仍然有空白 Mel上面的评论(使用plt.tight_layout()
)在许多情况下都有效,但有时您需要更多的控制。要更精细地操纵轴(有用,例如,当您有许多色条或双
-ned轴时),可以使用plt.subplot\u adjust()
或GridSpec
对象
GridSpec
对象允许您指定各个轴的水平和垂直范围,以及它们的比例宽度、高度和间距<代码>子图\u adjust()在轴上绘制内容后移动轴。我更喜欢使用第一个选项,但两者都是
这也可能有助于摆弄你的图形大小。如果你有大量的空格宽度,请缩小图形的宽度
下面是我用来设置最近的绘图的一些示例代码:
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()
的轴参数使用gridspec.gridspec,问题已经解决…:-)