matplotlib savefig bbox_inches ='tight'不会忽略不可见的轴 [英] matplotlib savefig bbox_inches = 'tight' does not ignore invisible axes

查看:2120
本文介绍了matplotlib savefig bbox_inches ='tight'不会忽略不可见的轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Matplotlib的savefig()函数中设置bbox_inches ='tight'时,它将尝试找到最紧密的边界框,该边界框将图形窗口中的所有内容封装在一起.不幸的是,最紧密的边界框似乎包含不可见的轴.

When you set bbox_inches = 'tight' in Matplotlib's savefig() function, it tries to find the tightest bounding box that encapsulates all the content in your figure window. Unfortunately, the tightest bounding box appears to include invisible axes.

例如,下面是一个片段,其中设置bbox_inches ='tight'可以根据需要进行操作:

For example, here is a snippet where setting bbox_inches = 'tight' works as desired:

import matplotlib.pylab as plt
fig = plt.figure(figsize = (5,5))
data_ax = fig.add_axes([0.2, 0.2, 0.6, 0.6])
data_ax.plot([1,2], [1,2])
plt.savefig('Test1.pdf', bbox_inches = 'tight', pad_inches = 0)

产生:

已保存的pdf的边界与内容的边界相对应.很好,除了我喜欢使用一组不可见的图形轴来放置批注.如果不可见轴超出可见内容的范围,则pdf范围大于可见内容.例如:

The bounds of the saved pdf correspond to the bounds of the content. This is great, except that I like to use a set of invisible figure axes to place annotations in. If the invisible axes extend beyond the bounds of the visible content, then the pdf bounds are larger than the visible content. For example:

import matplotlib.pylab as plt
fig = plt.figure(figsize = (5,5))
fig_ax = fig.add_axes([0, 0, 1, 1], frame_on = False)
fig_ax.xaxis.set_visible(False)
fig_ax.yaxis.set_visible(False)
data_ax = fig.add_axes([0.2, 0.2, 0.6, 0.6])
data_ax.plot([1,2], [1,2])
plt.savefig('Test2.pdf', bbox_inches = 'tight', pad_inches = 0)

生产

如何强制savefig()忽略图形窗口中的不可见项?我想出的唯一解决方案是自己计算边界框,然后将bbox明确指定为savefig().

How can I force savefig() to ignore invisible items in the figure window? The only solution I have come up with is to calculate the bounding box myself and explicitly specify the bbox to savefig().

如果有问题,我将在Mac OS X 10.8.5上的Python 2.7.3下运行Matplotlib 1.2.1.

In case it matters, I am running Matplotlib 1.2.1 under Python 2.7.3 on Mac OS X 10.8.5.

推荐答案

backend_bases.py中的相关函数(由canvas.print_figure调用,由figure.savefig调用以生成边界框):

The relevant function (called by canvas.print_figure which is called by figure.savefig to generate the bounding box) in backend_bases.py:

def get_tightbbox(self, renderer):
    """
    Return a (tight) bounding box of the figure in inches.

    It only accounts axes title, axis labels, and axis
    ticklabels. Needs improvement.
    """

    bb = []
    for ax in self.axes:
        if ax.get_visible():
            bb.append(ax.get_tightbbox(renderer))

    _bbox = Bbox.union([b for b in bb if b.width != 0 or b.height != 0])

    bbox_inches = TransformedBbox(_bbox,
                                  Affine2D().scale(1. / self.dpi))

    return bbox_inches

即使轴上没有可见的美术师(artist.get_visible() == False或简单的透明艺术家),唯一决定是否轴可见"的因素是ax.get_visible()是否返回true.

The only consideration that goes into deciding if an axes is 'visible' is if ax.get_visible() returns true, even if you have no visible (either artist.get_visible() == False or simple transparent) artists in the axes.

您观察到的边界框行为是正确的行为.

The bounding box behavior you observe is the correct behavior.

这篇关于matplotlib savefig bbox_inches ='tight'不会忽略不可见的轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆