简体   繁体   English

在matplotlib中使用tight_layout()并使用不完整的轴数组:

[英]use of tight_layout() in matplotlib with incomplete axis array:

I am creating PDFs of an array of axes. 我正在创建轴数组的PDF。 Sometimes the page is not full, ie not all axes have data. 有时页面未满,即并非所有轴都有数据。 In this case, I want the unused axes not to show on the PDF. 在这种情况下,我希望未使用的轴不显示在PDF中。 But I want the layout to be the same as if they were being used. 但是我希望布局与使用它们时相同。 I'm using tight_layout() to get non-overlapping axes and ylabels. 我正在使用tight_layout()来获取不重叠的轴和ylabel。

The following code shows first the case where the axes are used, then what happens if I delete the unused ones (tight_layout does not work properly), and then, if I instead just set them not to be visible, tight_layout() fails with a 以下代码首先显示了使用轴的情况,然后显示了删除未使用的轴(tight_layout无法正常工作),然后,如果我只是将其设置为不可见,则tight_layout()失败并显示

AttributeError: 'NoneType' object has no attribute 'is_bbox' AttributeError:“ NoneType”对象没有属性“ is_bbox”

error. 错误。

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', )

I want the layout of the first version, but without the extra axes visible. 我想要第一个版本的布局,但没有可见的额外轴。

You could create the axes independently of the figure. 您可以独立于图形创建轴。 I would also recommend this method because you have more control over the axes, for example you could have different shaped axes. 我还建议使用此方法,因为您可以对轴进行更多控制,例如可以使用不同形状的轴。

Code: 码:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
for ii in range(5):
    ax = fig.add_subplot(4,3,ii+1)
    ax.scatter(np.random.random(5),np.random.random(5))
    ax.set_xlabel('xlabel')
    ax.set_ylabel('ylabel')
fig.tight_layout()
fig.show()

Result: 结果: 在此处输入图片说明

The second case of deleting the axes works fine if it is used on its own (without the code from the first case executed) and if the figure is first saved and then shown, 如果删除轴的第二种情况是单独使用的(不执行第一种情况的代码),并且先保存并随后显示图形,则可以很好地工作,

fig,axs=prep_figure()
for ii in range(5,12):
    fig.delaxes(axs[ii])
plt.tight_layout()
plt.savefig('tmpd.pdf', )
plt.show()

The third case works fine if again, the figure is saved before showing it, and instead of making it invisible, turning the axes off via ax.axis("off") . 第三种情况很好用,如果再次显示该图形,则在保存该图形之前将其保存,而不是使其不可见,而是通过ax.axis("off")轴。

fig,axs=prep_figure()
for ii in range(5,12):
    axs[ii].axis("off")
plt.tight_layout()
plt.savefig('tmph.pdf', )
plt.show()

The created pdf is the same in both cases: 两种情况下创建的pdf都相同:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM