【发布时间】:2018-04-25 11:51:04
【问题描述】:
在 matplotlib 2.0.2 中使用默认的 mathtext 而不是 LaTeX 数学渲染引擎时,我观察到了错误。似乎mathtext无法识别某些字形(在我的情况下是减号和乘号)。真正奇怪的是,仅当这些特定字形出现在刻度标签中时才会发生错误。当我故意输入一些数学表达式时,例如图标题,效果很好。
考虑以下示例和生成的图像:
import matplotlib
import matplotlib.pyplot as plt
# Customize matplotlib
matplotlib.rcParams.update({# Use mathtext, not LaTeX
'text.usetex': False,
# Use the Computer modern font
'font.family': 'serif',
'font.serif': 'cmr10',
'mathtext.fontset': 'cm',
})
# Plot
plt.semilogy([-0.03, 0.05], [0.3, 0.05])
plt.title(r'$-6\times 10^{-2}$')
plt.savefig('test.png')
如图所示,刻度标签中的乘法和一些减号已被其他字符替换。如果我使用 LaTeX(通过将 'text.usetex' 设置为 True),一切都会很好地呈现。为什么会发生这种情况,更重要的是,如何在不从 mathtext 更改为 LaTeX 的情况下修复它?
附加信息
这是运行示例代码时打印的警告:
mathtext.py:866: MathTextWarning: Font 'default' does not have a glyph for '\times' [U+d7]
MathTextWarning)
mathtext.py:867: MathTextWarning: Substituting with a dummy symbol.
warn("Substituting with a dummy symbol.", MathTextWarning)
请注意,出现在指数中的减号会正确呈现。如果我忽略'mathtext.fontset': 'cm',这些也可能不会呈现,产生另一个类似的警告:
mathtext.py:866: MathTextWarning: Font 'default' does not have a glyph for '-' [U+2212]
MathTextWarning)
mathtext.py:867: MathTextWarning: Substituting with a dummy symbol.
warn("Substituting with a dummy symbol.", MathTextWarning)
另外,如果我在rcParams 中包含'axes.unicode_minus': False(并保留'mathtext.fontset': 'cm'),所有减号都会正确呈现,但乘号仍然存在问题。
在旧版本的 matplotlib 上,乘号错误似乎不是问题(我测试过 1.5.1、1.4.3 和 1.3.1)。但是,这些 matplotib 坚持只在 10⁻²、10⁻¹、1、10、10² 等处生成刻度标签,因此不需要乘号。
错误报告
这已作为 bug report 提交给 Matplotlib。
【问题讨论】:
-
这似乎是
cmr10字体的问题。如果您改用'font.serif': 'Times New Roman',,它会显示得很好。如果您认为 cmr10 也应该可以工作,最好将其发布到 matplotlib issue tracker。 -
@ImportanceOfBeingErnest 是的,更改为其他字体会有所帮助。但是,我非常想要 matplotlib 附带的类似“计算机现代”的字体。我无法找到保证所有 matplotlib 安装都可用的完整字体列表。
-
我想如果字体在标题中正确呈现但在轴标签中不正确呈现,则可能被视为错误。所以我想说,在它所属的地方报告它仍然是最好的主意。
标签: python matplotlib unicode latex text-rendering