Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

python - How to disable bbox_inches='tight' when working with matplotlib inline in ipython notebook

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.

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~

EDIT

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:)

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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...