当绘制两个(或更多)子图时,图中(所有四个边上)有一大片空白,如下所示:
下面是我用来绘制它的代码。在
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
-ned轴时),可以使用plt.subplots_adjust()
或GridSpec
对象。在GridSpec
对象允许您指定各个轴的水平和垂直范围,以及它们的比例宽度和高度&间距。subplots_adjust()
在你已经在轴上绘图之后移动轴。我更喜欢使用第一个选项,但两者都是documentedwell。在它也可能有助于愚弄你的身材大小。如果你有很多空白宽度,把图形的宽度缩小。在
下面是一些我用来设置最近绘图的示例代码:
结果是:
然后,如果需要colorbar,请将
GridSpec
中的right
参数调整为0.85,并将fig.add_axes()
与列表[left_lim, bottom, width, height]
一起使用,并将其用作fig.colorbar()
的轴参数相关问题 更多 >
编程相关推荐