本文介绍了matplotlib-更改figsize但保持fontsize不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示几个大小不同的图形,以确保打印图形时文本始终具有相同的大小.我该如何实现?

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

如何设置字体大小,以便在打印时,图1中的标题和轴刻度标签与图2中的字体具有相同的大小?

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?

推荐答案

在这种情况下,字体大小将相同(即也为10磅).

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

但是,在Jupyter Notebook中,如果数字过宽,则数字可能会以不同的大小显示,请参见下文:

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

请注意,以磅为单位的字体大小具有线性比例,因此,如果您希望字母的大小恰好是其两倍,则需要输入以磅为单位的大小恰好是两倍(例如20pt).这样,如果您希望以原始大小的50%(长度和宽度,而不是面积)打印第二个图形,则字体将是相同大小.

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.

tcaswell 所述,bbox_inches='tight'有效地更改了保存图形的大小,因此大小不同从您设置为figsize的位置开始.由于这可能从某些图形中裁剪出比其他图形更多的空白,因此对于给定的宽高比,对象和字体的相对大小最终可能会有所不同.

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.

这篇关于matplotlib-更改figsize但保持fontsize不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:39
查看更多