我希望使用 latex 脚本 $\theta$ 来标记 matplotlib 图中的轴和图例。所以我使用下面的代码。
y = np.linspace(0, 2*np.pi, 200)
plt.plot(y, np.sin(y)*np.cos(y), lw=3, label='$sin(\theta) cos(\theta)$')
plt.xlabel('$\theta$', fontsize=40)
plt.ylabel('$P(\theta,t)$', fontsize=40)
该代码适用于 $\Theta$,但我不想更改变量名称。 $\rho$、$\tau$、$\alpha$、$\beta$ 也会出现同样的问题。请提供一些有关如何摆脱此问题的提示。
感谢您的帮助。
最佳答案
使用原始字符串,否则字符串中的 \t
将被解释为 TAB
。
Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters.
您通常应该对包含 Latex 表达式的字符串执行此操作,因为它们通常包含反斜杠。
y = np.linspace(0, 2*np.pi, 200)
plt.plot(y, np.sin(y)*np.cos(y), lw=3, label=r'$sin(\theta) cos(\theta)$')
plt.xlabel(r'$\theta$', fontsize=40)
plt.ylabel(r'$P(\theta,t)$', fontsize=40)
关于python - matplotlib 中的 Latex 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45073493/