【问题标题】:Accented Latex letters in Matplotlib label [duplicate]Matplotlib标签中的重音乳胶字母[重复]
【发布时间】:2021-05-16 00:19:39
【问题描述】:

我的问题与this question有关。

我想在 Matplotlib 图中将 $\tilde{b}$ 显示为 ylabel。

MWE

import matplotlib
import matplotlib.pyplot as plt
matplotlib.rc('text', usetex=True)
plt.figure(1)
plt.plot(np.array([0, 1]), np.array([0, 1]))
plt.ylabel('$\tilde{b}$')
plt.show()

结果只是显示

ilde b

在轴上。我认为这与 b 是辅音这一事实有关,但它也不适用于元音。

我能做什么?

【问题讨论】:

标签: python matplotlib latex axis-labels


【解决方案1】:

您得到该结果是因为在您要呈现为标签的字符串 '$\tilde{b}$' 中,Python 将 \t 识别为特殊字符:a horizontal tab

它不是唯一的特殊字符。另一个突出的例子是\n,它代表一个换行符。特殊字符的完整列表可以在 Python 语言参考的 "String and Bytes literals" 部分找到。

部分"Strings" 在 Python 教程注释中:

如果您不希望以 \ 开头的字符被解释为特殊字符,您可以通过在第一个引号前添加 r 来使用原始字符串:

所以你得到了想要的输出 if 而不是

plt.ylabel('$\tilde{b}$')

你使用

plt.ylabel(r'$\tilde{b}$')

不管怎样,代码中的 matplotlib.rc('text', usetex=True) 行(除了 Matplotlib 之外还需要安装 LaTeX)不是重现行为所必需的。

【讨论】:

    猜你喜欢
    • 2018-05-09
    • 2015-02-17
    • 2017-08-12
    • 1970-01-01
    • 2020-01-02
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多