Matplotlib 标签中的重音乳胶字母
Accented Latex letters in Matplotlib label
我的问题与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 是辅音有关,但它对元音不起作用。
我能做什么?
您得到该结果是因为在要呈现为标签的字符串中,'$\tilde{b}$'
、Python 将 \t
识别为特殊字符:a horizontal tab。
它不是唯一的特殊字符。另一个突出的例子是 \n
,它代表一个换行符。特殊字符的完整列表可以在 Python 语言参考的 "String and Bytes literals" 部分找到。
第 "Strings" 节
在 Python 教程注释中:
If you don’t want characters prefaced by \ to be interpreted as special characters, you can use raw strings by adding an r before the first quote:
因此,如果不是
,您将获得所需的输出
plt.ylabel('$\tilde{b}$')
你用
plt.ylabel(r'$\tilde{b}$')
对于它的价值,代码中的 matplotlib.rc('text', usetex=True)
行(除了 Matplotlib 之外还需要安装 LaTeX)对于重现该行为是不必要的。
我的问题与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 是辅音有关,但它对元音不起作用。
我能做什么?
您得到该结果是因为在要呈现为标签的字符串中,'$\tilde{b}$'
、Python 将 \t
识别为特殊字符:a horizontal tab。
它不是唯一的特殊字符。另一个突出的例子是 \n
,它代表一个换行符。特殊字符的完整列表可以在 Python 语言参考的 "String and Bytes literals" 部分找到。
第 "Strings" 节 在 Python 教程注释中:
If you don’t want characters prefaced by \ to be interpreted as special characters, you can use raw strings by adding an r before the first quote:
因此,如果不是
,您将获得所需的输出plt.ylabel('$\tilde{b}$')
你用
plt.ylabel(r'$\tilde{b}$')
对于它的价值,代码中的 matplotlib.rc('text', usetex=True)
行(除了 Matplotlib 之外还需要安装 LaTeX)对于重现该行为是不必要的。