我正在创建一个轴数组的PDF。有时页面未满,即并非所有轴都有数据。在这种情况下,我希望未使用的轴不显示在PDF上。但我希望布局与使用布局相同。我正在使用tight_layout()来获取非重叠的坐标轴和ylabels。在matplotlib中使用tight_layout()与不完整的轴数组:
下面的代码首先显示了使用坐标轴的情况,然后如果我删除未使用的坐标轴(tight_layout不能正常工作)会发生什么情况,如果我只是将它们设置为不可见,那么tight_layout( )不合格
AttributeError: 'NoneType' object has no attribute 'is_bbox'
错误。
import numpy as np
import matplotlib.pyplot as plt
def prep_figure():
plt.close('all')
fig, axs = plt.subplots(4,3, figsize=(11,8.5))
axs=np.concatenate(axs)
for ii in range(5):
axs[ii].plot([1,2,3],[-10,-1,-10])
axs[ii].set_ylabel('ylabel')
axs[ii].set_xlabel('xlabel')
return fig,axs
fig,axs=prep_figure()
plt.tight_layout()
plt.show()
plt.savefig('tmp.pdf',)
# Try deleting extra axes
fig,axs=prep_figure()
for ii in range(5,12):
fig.delaxes(axs[ii])
plt.tight_layout()
plt.show()
plt.savefig('tmpd.pdf',)
# Try hiding extra axes
fig,axs=prep_figure()
for ii in range(5,12):
axs[ii].set_visible(False)
plt.tight_layout()
plt.show()
plt.savefig('tmph.pdf',)
我想要第一个版本的布局,但没有额外的轴可见。