\ frac {} {}对我来说不适用于pylab

Dan*_*iel 4 python matplotlib tex

我很擅长使用p​​ython,尤其是numpy和matplotlib.运行下面的代码(没有\frac{}{}部件工作正常)会产生错误:

Normalized Distance in Chamber ($
                             rac{x}{L}$)
                            ^
Expected end of text (at char 32), (line:1, col:33)
Run Code Online (Sandbox Code Playgroud)

数学模式似乎适用于我尝试的其他一切(符号大多数,例如$\mu$工作正常并显示μ)所以我不确定这里发生了什么.我已经查找了其他人的代码示例,他们似乎\frac{}{}没有什么特别的使用,它工作正常.我不知道我在做什么不同.这是代码.谢谢您的帮助!

import numpy as np
import math
import matplotlib.pylab as plt

[ ... bunch of calculations ... ]

plt.plot(xspace[:]/L,vals[:,60])
plt.axis([0,1,0,1])
plt.xlabel('Normalized Distance in Chamber ($\frac{x}{L}$)')
plt.savefig('test.eps')
Run Code Online (Sandbox Code Playgroud)

此外,我确实查找\ f,它似乎是一个"逃脱角色",但我不知道这意味着什么或为什么它将在TeX模式下活跃.

Pav*_*sov 7

在许多语言中,反斜杠字母是一种输入难以输入的字符的方法.在这种情况下,它是一个"换页".例子:

\n — newline
\r — carriage return
\t — tab character
\b — backspace
Run Code Online (Sandbox Code Playgroud)

要禁用它,您需要转义反斜杠本身(反斜杠反斜杠是反斜杠)

'Normalized Distance in Chamber ($\\frac{x}{L}$)'
Run Code Online (Sandbox Code Playgroud)

或者使用禁用转义序列的"原始"字符串:

r'Normalized Distance in Chamber ($\frac{x}{L}$)'
Run Code Online (Sandbox Code Playgroud)

这与Python相关,而不是TeX.

有关Python字符串文字的文档