【发布时间】:2017-04-20 04:35:25
【问题描述】:
我的目标是为 matplotlib 图形中的所有文本(常规和数学)使用特定字体,例如 Nimbus Sans L 或 Tex Gyre Heros。这两种字体以 .otf 文件的形式提供。当我运行下面的代码时,正确生成了 png 图像,但在 pdf 中,标题和轴号的字母之间有额外的空间。似乎从默认的 10pt 更改字体大小会产生奇怪的缩放。
pdf 有问题:
png 好的:
我尝试过的: .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)
【问题讨论】:
标签: python matplotlib fonts