使用开放式字体和pdf导出在Matplotlib中进行字体缩放

时间:2017-04-20 04:35:25

标签: python matplotlib fonts

我的目标是在matplotlib图中使用特定字体,例如Nimbus Sans L或Tex Gyre Heros,用于所有文本(常规和数学)。这两种字体可用作.otf文件。当我运行下面的代码时,png图像生成正确,但在pdf中,标题和轴编号的字母之间有额外的空格。似乎从默认的10pt更改字体大小会产生奇怪的缩放。

带问题的pdf:

enter image description here

png好的:

enter image description here

我尝试过的: 它似乎与.ttf字体一起使用。

我正在使用的是:matplotlib 2.0.0,python 3.5.2

示例代码:

import matplotlib as mpl

import numpy as np
x = np.linspace(-3, 10, 100)
ymodel = x**2
ydata = ymodel + 20 * (np.random.rand(len(x)) - 0.5)

font = 'tex gyre heros'  # an open type font

mpl.rcParams['font.sans-serif'] = font
mpl.rc('mathtext', fontset='custom', it=font + ':italic')
mpl.rc('font', size=13)  # change font size from default 10

import matplotlib.pyplot as plt
plt.figure()    
plt.plot(x, ydata, 'o')
plt.plot(x, ymodel)
plt.xlabel("Something for the $x$ axis (units)")
plt.ylabel("The $y$ axis (units)")
plt.title("{}".format(font.upper()))
plt.text(0, 80, "Some math: $\chi_{13}=-3\pi\psi\Delta_A\\times\omega + x^2$")

plt.savefig("fig_" + font.lower() + ".pdf", bbox_inches='tight')
plt.savefig("fig_" + font.lower() + ".png", bbox_inches='tight', dpi=300)

1 个答案:

答案 0 :(得分:0)

这听起来像this issue discussion中描述的错误。如果是这样,bug has been patched和解决方案是将matplotlib升级到2.0.1或更高版本。