matplotlib - change figsize but keep fontsize constant

戏子无情 提交于 2021-01-04 05:56:57

问题


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')
plt.plot(range(0,10),range(0,10))
plt.show()


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

fig2 = plt.figure(figsize = (20,10))
plt.title('This is fig2')
plt.plot(range(0,10),range(0,10))
plt.show()

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?


回答1:


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

However, in Jupyter Notebook the figures may be displayed at a different size if they are too wide, see below:

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.



来源:https://stackoverflow.com/questions/39395616/matplotlib-change-figsize-but-keep-fontsize-constant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!