每当我想渲染LaTeX进行打印标签和文本时,我就一直遇到这种麻烦,这有时对某些符号有效,但对另一些符号无效。例如,在此处显示的脚本中,生成以下图:
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)
我尝试了其他符号,$ \ pi $可以正常工作,但$ \ theta $仅显示“ heta”而不带t。为什么有些符号可用于LaTeX而有些则不能,我感到困惑。
谢谢!
请记住,Python字符串中的某些字符具有特殊含义,例如,\r
用于回车,\t
用于制表符。这就是为什么您有时只得到奇怪结果的原因,因为\p
它没有特殊含义。因此,要么通过转义确保您的反斜杠被视为文字反斜杠:
plt.plot(a_t,rho,label = "$\\rho/\\rho_c$")
或使用原始字符串:
plt.plot(a_t,rho,label = r"$\rho/\rho_c$")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句