在matplotlib中对齐TeX公式

3 投票
1 回答
2883 浏览
提问于 2025-04-16 18:01

我在我的 matplotlib 图表上有以下几行代码,用来添加 TeX 注释:

import matplotlib.pyplot as plt
from matplotlib import rc

rc('text', usetex=True)
rc('font', family='serif')

r = 1
v = 2
i = 3

notes = r"\noindent$R_L = {0}\\ V_2 = {1}\\ I_2 = {2}$".format(r, v, i)
plt.annotate(notes, xy=(5,5), xytext=(7,7))
plt.show()

我想知道怎么让等号对齐。我试过几种方法,比如 \begin{align}& 的放置方式,但总是弄不好。

1 个回答

4

使用 eqnarray

这是你代码的一个简化版本:

#!/usr/bin/python
import matplotlib.pyplot as plt
from matplotlib import rc

rc('text', usetex=True)
rc('font', family='serif')

r = 1
v = 2
i = 3

plt.plot([1,2,3],[2,3,4],'ro-')

plt.text(2,2,r"\begin{eqnarray*}R_L&= 0\\ V_2&= 1\\ I_2&= 2\end{eqnarray*}")

#notes = r"\noindent$$R_L = {0}\\ V_2 = {1}\\ I_2 = {2}$$".format(r, v, i)
#plt.annotate(notes, xy=(5,5), xytext=(7,7))
plt.show()

为了让它正常工作,我需要安装 dvipng(在这里有建议,http://matplotlib.sourceforge.net/users/usetex.html

撰写回答