Python 对齐matplotlib文本框中的LaTeX数学文本

Python 对齐matplotlib文本框中的LaTeX数学文本,python,matplotlib,latex,Python,Matplotlib,Latex,我正在尝试生成一个文本框,其中包含在matplotlib框架中正确对齐的LaTeX代码行。我尝试使用格式对齐方法(即:{:您可以使用所述的eqnarrayenvironment) 使用align环境的替代解决方案: import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec import matplotlib.offsetbox as offsetbox custom_preamble = { "text

我正在尝试生成一个文本框,其中包含在
matplotlib
框架中正确对齐的LaTeX代码行。我尝试使用格式对齐方法(即:
{:您可以使用所述的
eqnarray
environment)


使用
align
环境的替代解决方案:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.offsetbox as offsetbox
custom_preamble = {
    "text.usetex": True,
    "text.latex.preamble": [
        r"\usepackage{amsmath}", # for the align enivironment
        ],
    }
plt.rcParams.update(custom_preamble)

# Figure top-level container. Weird size is because
# this is part of a larger code.
fig = plt.figure(figsize=(30, 25))
gs = gridspec.GridSpec(10, 12)
ax_t = plt.subplot(gs[4:6, 10:12])

# Some mock values.
cp_r = [0.001, 8.3, 0.18, 15.2, 5000, 0.3]
cp_e = [0.0005, 0.2, 0.11, 0.3, 200, 0.1]

# Remove axis from frame.
ax_t.axis('off')

# Text lines.
text1 = r'\begin{align*} '
text2 = r'y &= ' + str(cp_r[0]) + '\pm ' + str(cp_e[0]) + '\\\\'
text3 = r'\log(ret) &= ' + str(cp_r[1]) + '\pm ' + str(cp_e[1]) + '\\\\'
text4 = r'A_{{(B-C)}} &= ' + str(cp_r[2]) + '\pm ' + str(cp_e[2]) + '\\\\'
text5 = r'(n-N)_o &= ' + str(cp_r[3]) + '\pm ' + str(cp_e[3]) + '\\\\'
text6 = r'K_{{\odot}} &= ' + str(cp_r[4]) + '\pm ' + str(cp_e[4]) + '\\\\'
text7 = r'd_{{frac}} &= ' + str(cp_r[5]) + '\pm ' + str(cp_e[5])
text8 = r'\end{align*}'
text = text1 + text2 + text3 + text4 + text5 + text6 + text7 + text8

# Draw text box.
ob = offsetbox.AnchoredText(text, pad=1, loc=6, prop=dict(size=13))
ob.patch.set(alpha=0.85)
ax_t.add_artist(ob)

plt.savefig('out.png', dpi=300)

谢谢Benjamin!此代码因错误而崩溃:
LaTeX无法处理以下字符串:'lp'这是LaTeX生成的完整报告:
。不幸的是,最后一行下没有任何内容。基于此问题,我似乎缺少一些LaTeX包。这意味着此答案为代码添加了依赖项,而ich并不理想。我将尝试找出哪个软件包是。是的,我缺少
texlive latex extra
中明显存在的
type1cm.sty
。如果可能的话,我宁愿避免将此依赖项添加到代码中,但如果没有给出更好的答案,我会用警告将此项标记为已接受。我已经尝试将t最小化通过使用
eqnarray
我个人更喜欢使用
align
环境。但是这个解决方案可能与您安装的软件包兼容。@Gabriel该软件包在Ubuntu和Gentoo上似乎是:
,基本texlive安装不附带type1cm软件包。
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.offsetbox as offsetbox
from matplotlib import rc

rc('text', usetex=True)

# Figure top-level container. Weird size is because
# this is part of a larger code.
fig = plt.figure(figsize=(30, 25))
gs = gridspec.GridSpec(10, 12)
ax_t = plt.subplot(gs[4:6, 10:12])

# Some mock values.
cp_r = [0.001, 8.3, 0.18, 15.2, 5000, 0.3]
cp_e = [0.0005, 0.2, 0.11, 0.3, 200, 0.1]

# Remove axis from frame.
ax_t.axis('off')

# Text lines.
text1 = r'\begin{eqnarray*} '
text2 = r'y &=& ' + str(cp_r[0]) + '\pm ' + str(cp_e[0]) + '\\\\'
text3 = r'\log(ret) &=& ' + str(cp_r[1]) + '\pm ' + str(cp_e[1]) + '\\\\'
text4 = r'A_{{(B-C)}} &=& ' + str(cp_r[2]) + '\pm ' + str(cp_e[2]) + '\\\\'
text5 = r'(n-N)_o &=& ' + str(cp_r[3]) + '\pm ' + str(cp_e[3]) + '\\\\'
text6 = r'K_{{\odot}} &=& ' + str(cp_r[4]) + '\pm ' + str(cp_e[4]) + '\\\\'
text7 = r'd_{{frac}} &=& ' + str(cp_r[5]) + '\pm ' + str(cp_e[5])
text8 = r'\end{eqnarray*}'
text = text1 + text2 + text3 + text4 + text5 + text6 + text7 + text8

# Draw text box.
ob = offsetbox.AnchoredText(text, pad=1, loc=6, prop=dict(size=13))
ob.patch.set(alpha=0.85)
ax_t.add_artist(ob)

plt.savefig('out.png', dpi=300)
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.offsetbox as offsetbox
custom_preamble = {
    "text.usetex": True,
    "text.latex.preamble": [
        r"\usepackage{amsmath}", # for the align enivironment
        ],
    }
plt.rcParams.update(custom_preamble)

# Figure top-level container. Weird size is because
# this is part of a larger code.
fig = plt.figure(figsize=(30, 25))
gs = gridspec.GridSpec(10, 12)
ax_t = plt.subplot(gs[4:6, 10:12])

# Some mock values.
cp_r = [0.001, 8.3, 0.18, 15.2, 5000, 0.3]
cp_e = [0.0005, 0.2, 0.11, 0.3, 200, 0.1]

# Remove axis from frame.
ax_t.axis('off')

# Text lines.
text1 = r'\begin{align*} '
text2 = r'y &= ' + str(cp_r[0]) + '\pm ' + str(cp_e[0]) + '\\\\'
text3 = r'\log(ret) &= ' + str(cp_r[1]) + '\pm ' + str(cp_e[1]) + '\\\\'
text4 = r'A_{{(B-C)}} &= ' + str(cp_r[2]) + '\pm ' + str(cp_e[2]) + '\\\\'
text5 = r'(n-N)_o &= ' + str(cp_r[3]) + '\pm ' + str(cp_e[3]) + '\\\\'
text6 = r'K_{{\odot}} &= ' + str(cp_r[4]) + '\pm ' + str(cp_e[4]) + '\\\\'
text7 = r'd_{{frac}} &= ' + str(cp_r[5]) + '\pm ' + str(cp_e[5])
text8 = r'\end{align*}'
text = text1 + text2 + text3 + text4 + text5 + text6 + text7 + text8

# Draw text box.
ob = offsetbox.AnchoredText(text, pad=1, loc=6, prop=dict(size=13))
ob.patch.set(alpha=0.85)
ax_t.add_artist(ob)

plt.savefig('out.png', dpi=300)