Laura Laura - 1 year ago 106
Python Question

matplotlib - change figsize but keep fontsize constant

I want to display several figures with different sizes, making sure that the text has always the same size when the figures are printed. How can I achieve that?

As an example. Let's say I have two figures:

import matplotlib.pylab as plt
import matplotlib as mpl

mpl.rc('font', size=10)

fig1 = plt.figure(figsize = (3,1))
plt.title('This is fig1')

mpl.rc('font', size=?)

fig2 = plt.figure(figsize = (20,10))
plt.title('This is fig2')

How can I set the fontsize in such way that when printed the title and axis ticklabels in fig1 will have the same size as those in fig2?

Answer Source

In this case, the font size would be the same (i.e. also 10 points).

Note that font size in points has a linear scale, so if you would want the size of the letters to be exactly twice as big, you would need to enter exactly twice the size in points (e.g. 20pt). That way, if you expect to print the second figure at 50% of the original size (length and width, not area), the fonts would be the same size.

But if the only purpose of this script is to make figures to then print, you would do best to set the size as desired (on paper or on screen), and then make the font size equal. You could then paste them in a document at that exact size or ratio and the fonts would indeed be the same size.

As noted by tcaswell, bbox_inches='tight' effectively changes the size of the saved figure, so that the size is different from what you set as figsize. As this might crop more whitespaces from some figures than others, the relative sizes of objects and fonts could end up being different for a given aspect ratio.