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

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

问题描述

在 ipython notebook 中使用 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.

然而,有时人们可能想要(暂时)禁用此功能,例如,当他想要手动保持两个图形垂直对齐时(假设我们不想在这里使用 subplot):

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),由于 bbox_inches 触发自动布局调整后,由于 yticklabels 中要显示更多数字,第二个图将具有更大的左边距(和更小的右边距)savefig() 中的 ='tight' 选项,由 notebook 内部调用以生成嵌入的 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':None}

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 notebook 中使用内联 matplotlib 时如何禁用 bbox_inches='tight'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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