可以使用rc函数将TeX公式插入绘图中
importmatplotlib.pyplotas plt plt.rc(usetex = True)
或访问rcParams:
importmatplotlib.pyplotas plt params = {'tex.usetex': True} plt.rcParams.update(params)
TeX\对命令和符号使用反斜杠,这可能与Python字符串中的特殊字符冲突。为了在Python字符串中使用文字反斜杠,必须对它们进行转义或将其合并到原始字符串中:
plt.xlabel('\\alpha') plt.xlabel(r'\alpha')
下图可以通过代码生成
importmatplotlib.pyplotas plt plt.rc(usetex = True) x = range(0,10) y = [t**2 for t in x] z = [t**2+1 for t in x] plt.plot(x, y, label = r'$\beta=\alpha^2$') plt.plot(x, z, label = r'$\beta=\alpha^2+1$') plt.xlabel(r'$\alpha$') plt.ylabel(r'$\beta$') plt.legend(loc=0) plt.show()
不支持显示的方程式(例如$$...$$或\begin{equation}...\end{equation})。不过,使用可以显示数学样式\displaystyle。
要加载乳胶包装,请使用以下tex.latex.preamble参数:
params = {'text.latex.preamble' : [r'\usepackage{siunitx}', r'\usepackage{amsmath}']} plt.rcParams.update(params)
但是请注意,示例matplotlibrc文件中的警告:
#text.latex.preamble : # IMPROPER USE OF THIS FEATURE WILL LEAD TO LATEX FAILURES # AND IS THEREFORE UNSUPPORTED. PLEASE DO NOT ASK FOR HELP # IF THIS FEATURE DOES NOT DO WHAT YOU EXPECT IT TO. # preamble is a comma separated list of LaTeX statements # that are included in the LaTeX document preamble. # An example: # text.latex.preamble : \usepackage{bm},\usepackage{euler} # The following packages are always loaded with usetex, so # beware of package collisions: color, geometry, graphicx, # type1cm, textcomp. Adobe Postscript (PSSNFS) font packages # may also be loaded, depending on your font settings