python - 在 matplotlib 文本框中对齐 LaTeX 数学文本

标签 python matplotlib latex

我正在尝试生成一个文本框,其中包含在 matplotlib 中正确对齐的 LaTeX 代码行框架。我尝试使用格式对齐方法(即:{:<11}),但它似乎在数学模式下不起作用。

这是我得到的输出:

enter image description here

哪里=符号应该全部向右对齐(参见下面的 MWE)。

如何使用 = 生成这样的文本框?标志是否正确对齐?


MWE:

(不要介意奇怪的图形比例和文本框放置,这是我裁剪生成此 MWE 的更大代码的一部分)

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.offsetbox as offsetbox

# 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.
text2 = r'{:<11}'.format('$y$') + \
    r'$=\, {} \pm {}$'.format(cp_r[0], cp_e[0])
text3 = r'{:<11}'.format('$log(ret)$') + \
    r'$=\, {} \pm {}$'.format(cp_r[1], cp_e[1])
text4 = r'{:<11}'.format('$A_{{(B-C)}}$') + \
    r'$=\, {} \pm {}$'.format(cp_r[2], cp_e[2])
text5 = r'{:<11}'.format('$(n-N)_o$') + \
    r'$=\, {} \pm {}$'.format(cp_r[3], cp_e[3])
text6 = r'{:<11}'.format('$K_{{\odot}}$') + \
    r'$=\, {} \pm {}$'.format(cp_r[4], cp_e[4])
text7 = r'{:<11}'.format('$d_{{frac}}$') + \
    r'$=\, {} \pm {}$'.format(cp_r[5], cp_e[5])
text = text2 + '\n' + text3 + '\n' + text4 + '\n' + text5 + '\n' + text6 + \
    '\n' + text7

# 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)

最佳答案

您可以按照描述使用eqnarray 环境here .

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)

使用 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)

关于python - 在 matplotlib 文本框中对齐 LaTeX 数学文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30515888/

相关文章:

python - 写入 JSON 时如何不转义反斜杠

python - Scrapy 数据流以及项目和项目加载器

python - 在 matplotlib 中相对于图形移动轴

python - 如何在 matplotlib 中的子图中选取一个点并在相邻子图中突出显示它(点区域的扩展)

latex - Pandoc、markdown、powerpoint : support for equations?

python - Google App Engine 批量下载

python - 将纪元时间戳列转换为带时区的日期时间

python - 如何在 matplotlib python 中定义边界?

math - 如何在 Latex 中将字符放置在函数下方?

latex ,fancyhdr