简体   繁体   English

LaTeX无法处理matplotlib文本

[英]LaTeX not working on matplotlib text

I've been having this trouble for a long time whenever I want to render LaTeX for plot labelling and text, it works sometimes for some symbols but not others. 每当我想渲染LaTeX来绘制图标签和文本时,我就一直遇到这种麻烦,有时它对某些符号有效,但对另一些符号无效。 For example in my script shown here generates the plot below: 例如,在此处显示的脚本中,生成以下图:

from matplotlib import rc
plt.rc('text', usetex=True)
plt.plot(a_t,asol[:,0],label ='$\psi$')
plt.plot(a_t,rho,label ="$\rho/\rho_c$")
plt.xlabel(r"$\xi$",fontsize=15)
from matplotlib.legend_handler import HandlerLine2D
plt.legend(loc='upper left',prop={'size':12},numpoints=1)

plt_result

I've tried other symbols, $\\pi$ works okay but $\\theta$ only shows "heta" without the t. 我尝试了其他符号,$ \\ pi $可以正常工作,但$ \\ theta $仅显示“ heta”而不带t。 I'm confused by why some symbols works for LaTeX and some don't. 为什么有些符号可用于LaTeX而有些符号却无法使用,我感到困惑。

Thanks! 谢谢!

Remember that certain characters in Python strings have special meanings, eg \\r for carriage return, \\t for tab. 请记住,Python字符串中的某些字符具有特殊含义,例如\\r表示回车, \\t表示tab。 That's why you're only getting the strange results some of the time, since \\p doesn't have a special meaning. 这就是为什么有时候您只得到奇怪结果的原因,因为\\p没有特殊含义。 So either make sure your backslashes are treated as literal backslashes by escaping them: 因此,要么通过转义确保您的反斜杠被视为文字反斜杠:

plt.plot(a_t,rho,label = "$\\rho/\\rho_c$")

Or use raw strings: 或使用原始字符串:

plt.plot(a_t,rho,label = r"$\rho/\rho_c$")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM