如何在ipython笔记本中使用matplotlib内联时禁用bbox_inches ='tight' [英] How to disable bbox_inches='tight' when working with matplotlib inline in ipython notebook

查看:148
本文介绍了如何在ipython笔记本中使用matplotlib内联时禁用bbox_inches ='tight'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ipython笔记本中使用matplotlib内联后端时,默认行为是使用bbox_inches ='tight'通过savefig()在内部生成嵌入式png图像。这消除了轴周围的空白,并且在大多数情况下都很好。

When work with matplotlib inline backend in ipython notebook, the default behavior is using bbox_inches='tight' to generate the embedded png image internally via savefig(). This eliminates the whitespace around the axes and is great in most cases.

但是,有时人们可能希望(暂时)禁用此功能,例如,当(s)他想手动保持两个数字垂直对齐(假设我们不想在这里使用子图):

However, sometimes one might want to (temporarily) disable this feature, for example, when (s)he wants to manually keep two figures vertically aligned (assume that we don't want to use subplot here):

%matplotlib inline
from pylab import *
plot(rand(100))
subplots_adjust(left=0.2) # Has no effect with inline, but works as expected with qt
figure()
plot(rand(100)*10000) # Will result in a larger left margin for this figure...
subplots_adjust(left=0.2)

那么如何禁用此行为?谢谢〜

So how to disable this behavior? Thanks~

为了使这里涉及的问题更加明确(感谢Anzel),第二个数字,由于更多的数字要显示在yticklabels中,在savefig()中的bbox_inches ='tight'选项触发自动布局调整后,左边距会更大(右边距更小),这是由笔记本内部调用以生成嵌入式png输出。它会有效地截断我故意用subplots_adjust()创建的任何额外空间,这样第二个数字似乎会向右移动,而不是与第一个数字垂直对齐。

To make the issue involved here more explicit (thanks to Anzel), the 2nd figure, due to more digits to be displayed in yticklabels, will have larger left margin (and smaller right margin) after automatic layout adjustment triggered by the bbox_inches='tight' option in savefig(), which is internally called by notebook to generate the embedded png output. It will effectively truncate any additional space I intentionally make with subplots_adjust(), so that the 2nd figure will seem to be shifted to the right, and not vertically "aligned" with the 1st figure.

很容易看出我的意思---只需尝试上面的代码片段:)

It is easy to see what I mean---just try the code snippet above:)

我之所以不使用子图/子图这里(参见对Anzel的回答的评论)是,在这个特殊情况下,这两个数字实际上是由数十个小的子图组成,加上一些额外的格式/标签。将它们合并为一个更大的子图阵列并非易事......

The reason why I'm not using subplot/subplots here (see comments to Anzel's answer) is that in this particular case, the two figures are acutally themselves composed of tens of small subplots, plus some additional formatting/labeling. Merging them into one larger array of subplots is not trivial...

推荐答案

这里有一个更全面的答案:Matplotlib和Ipython-notebook:准确显示将要出现的数字已保存

There's a fuller answer here: Matplotlib and Ipython-notebook: Displaying exactly the figure that will be saved

诀窍是关闭ipython中的 bbox_inches ='tight'设置。暂时做起来有点尴尬,但只是在一个块中运行IPython魔术:%config InlineBackend.print_figure_kwargs = {'bbox_inches':无}

The trick is to turn off the bbox_inches='tight' setting in ipython. It's a bit awkward to do temporarily, but just run the IPython magic in a block: %config InlineBackend.print_figure_kwargs = {'bbox_inches':None}

如果你想切换回正常方式,轴标签自动永不切断,你可以运行%config InlineBackend.print_figure_kwargs = {'bbox_inches':'tight '} 但它必须位于需要精确边界框的绘图块之后。

If you want to switch back to the normal way, where axis labels are automatically never cut, you can run %config InlineBackend.print_figure_kwargs = {'bbox_inches':'tight'} but it has to be after the block where you do the plotting that needs precise bounding boxes.

这篇关于如何在ipython笔记本中使用matplotlib内联时禁用bbox_inches ='tight'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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