使用 LaTeX 进行文本渲染Matplotlib 可以使用 LaTeX 来渲染文本。这可以通过在您的 rcParams 中设置 text.usetex : True 来激活,或者通过将 usetex 属性设置为 True 来激活单个 Text 对象。通过 LaTeX 处理文本比 Matplotlib 非常强大的 mathtext 速度更慢,但更灵活,因为可以使用不同的 LaTeX 包(字体包、数学包等)。结果可能很惊人,尤其是在您注意在图形中使用与主文档相同的字体时。 Matplotlib 的 LaTeX 支持需要一个可用的 LaTeX 安装。对于 *Agg 后端,还需要 dvipng;对于 PS 后端,还需要 PSfrag、dvips 和 Ghostscript。对于 PDF 和 SVG 后端,如果存在 LuaTeX,它将用于加速一些后处理步骤,但请注意,它不用于解析 TeX 字符串本身(只支持 LaTeX)。这些外部依赖项的可执行文件必须全部位于您的 PATH 中。
- import numpy as np
- import matplotlib.pyplot as plt
- from matplotlib import rc
- rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
- rc('text', usetex=True)
- #rc('text.latex', preamble=r'\usepackage[eulergreek]{sansmath}\sansmath')
- t = np.arange(0.0, 2.0, 0.01)
- s = np.sin(2*np.pi*t)
- figure()
- plt.plot(t,s)
- plt.title(r'$\alpha_i > \beta_i$', fontsize=20)
- plt.text(1, -0.6, r'$\displaystyle\sum_{i=0}^\infty x_i$', fontsize=20)
- plt.text(0.6, 0.6, r'$\mathcal{A}\mathrm{sin}(2 \omega t)$',
- fontsize=20)
- plt.xlabel('time (s)')
- plt.ylabel('volts (mV)')
- plt.savefig('fig_latex.pdf')
复制代码
|