【问题标题】:Font scaling in Matplotlib with open type fonts and pdf exportMatplotlib 中使用开放式字体和 pdf 导出的字体缩放
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-14
      • 1970-01-01
      • 1970-01-01
      • 2010-10-26
      • 2014-04-01
      • 2011-04-05
      • 2014-07-24
      • 2020-04-10
      相关资源
      最近更新 更多