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

标签 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)

最佳答案

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

关于python - 使用开放式字体和 pdf 导出在 Matplotlib 中进行字体缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43510274/

相关文章:

python - 如何使用 moto @mock_dynamodb2 模拟失败的操作?

python3.5 --version 命令未找到 CentOS 7

python - 自定义激活函数 Keras : Applying different activation to different layers

python - 使用 Matplotlib 旋转矩阵

python - Pyplot 颜色不符合预期

c# - 是什么导致 Calibri 在 9 到 14 pt 之间丢失 ClearType?

css - @font-face 在 Visual Studio 中无法识别

html - OpenType 字体和 TrueType 字体。有什么不同?

python - numpy 数组比较的快捷评估

python - Jupyter Notebook 不会导入 matplotlib,尽管它似乎已安装