Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pad_inches=0 and bbox_inches="tight" makes the plot smaller than declared figsize

I am producing a publication-quality plot to be embedded in latex and I would like to be very precise in terms of sizes and fonts (so that fonts are of the same size in the article as in the plot). To prevent the plot from scaling in latex I would like to have it exact size, but I cannot. Here is my code:

import matplotlib.pyplot as plt from matplotlib import rc, rcParams from numpy import sin  rc('text', usetex=True) rc('font', family='serif', serif='Computer Modern Roman', size=8) rc('legend', fontsize=10)  width_in = 5 fig = plt.figure(1, figsize=(width_in, 2)) ax = fig.add_subplot(111) ax.plot(range(0,100), sin(range(0,100)))  fig.tight_layout() fig.savefig('test.eps', bbox_inches='tight', pad_inches=0) plt.close() 

The problem is with bbox_inches='tight' and pad_inches=0. Adding those options makes my plot 4.76 inches wide instead of declared 5 inches. But I want them to save space. So how to solve it?

Edit: Well, the answers suggest to remove bbox_inches='tight' and pad_inches=0 and use just the tight_layout(). Then the images is of right size, however it has still some white padding around. I can remove it with fig.tight_layout(pad=0), but then the figure title it moved inside the box, which looks ugly. On the other hand I can use tight_layout(rect=[...]) and obtain the desired result, but it is a manual work to get the numbers right - I don't like it. Thus, currently I don't see any easy and general solution to my problem.

like image 257
cauchy Avatar asked Apr 16 '13 08:04

cauchy


People also ask

What is Bbox_inches =' tight?

The problem you are having is that bbox_inches='tight' just removes all of the extra white space around your figure, it does not actually re-arrange anything in your figure, after it has been rendered. You might need to tweak the parameters you pass to tight_layout (tutorial) to get your desired effect.

What is BBOX in Matplotlib?

BboxTransformTo is a transformation that linearly transforms points from the unit bounding box to a given Bbox. In your case, the transform itself is based upon a TransformedBBox which again has a Bbox upon which it is based and a transform - for this nested instance an Affine2D transform.

What is padding in Matplotlib?

pad: This parameter is used for padding between the figure edge and the edges of subplots, as a fraction of the font size. h_pad, w_pad: These parameter are used for padding (height/width) between edges of adjacent subplots, as a fraction of the font size.


1 Answers

The problem you are having is that bbox_inches='tight' just removes all of the extra white space around your figure, it does not actually re-arrange anything in your figure, after it has been rendered.

You might need to tweak the parameters you pass to tight_layout (tutorial) to get your desired effect.

Hopefully this gets you pointed in the right direction.

like image 93
tacaswell Avatar answered Sep 20 '22 17:09

tacaswell