在matplotlib文本框中对齐LaTeX数学文本 [英] Align LaTeX math text in matplotlib text box
本文介绍了在matplotlib文本框中对齐LaTeX数学文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试生成一个文本框,其中的LaTeX代码行在matplotlib
框架中正确对齐.我尝试使用格式对齐方法(即{:<11}
),但在数学模式下似乎不起作用.
I'm trying to generate a text box with lines of LaTeX code correctly aligned in a matplotlib
frame. I tried using the format alignment method (ie: {:<11}
) but it does not seem to work on math mode.
这是我得到的输出:
其中=
符号应都在右侧对齐(请参见下面的MWE).
where the =
symbols should all be aligned to the right (see MWE below).
如何生成这样的文本框,其=
符号正确对齐?
How can I produce such a text box with the =
signs correctly aligned?
MWE :
(不要介意怪异的数字比例和文本框放置,这是我裁剪生成此MWE的更大代码的一部分)
(Never mind the weird figure proportions and text box placing, this is part of a much larger code which I cropped to generate this 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)
推荐答案
查看全文