在matplotlib中对齐图例行

时间:2014-09-02 15:39:40

标签: python matplotlib tex

我正在用matplotlib做一个情节并为此创建一个图例(见下面的代码)。我希望图例行水平对齐,以便关系><对齐。试图调整类似问题的thisthis代码,我陷入困境。

我理解基本思路:使用\makebox[width][alignment]{math expression before aligment}<math expression after alignment作为标签,这样epsilon-expression使用的空间总是使用相同的空格并且与右边对齐,因此左边有自由空间。 / p>

但链接中使用的\hfill - 方法仅在hfill之前有文本时才有效,或者如果对齐是标准的(左)。解决方案必须非常接近,任何帮助将不胜感激。 这就是图例文本的样子 enter image description here

import numpy
from matplotlib import pyplot as plt

plt.rc('text', usetex=True)  # needed for interpeting tex strings, but changes appearence of axis-tick labels also
fig = plt.figure(1,figsize=(12.0, 8.0))
plt.ion()

# does not align the '<', '<' and '>' in the legend
# plt.plot(numpy.random.rand(10), label=r'\makebox[2cm][r]{$\varepsilon_i$}$< -\xi$')
# plt.plot(numpy.random.rand(10), label=r'\makebox[2cm][r]{$|\varepsilon_i|$}$< \xi$')
# plt.plot(numpy.random.rand(10), label=r'\makebox[2cm][r]{$\varepsilon_i$}$ > \xi$')

# \hfill doesnt change anything
# plt.plot(numpy.random.rand(10), label=r'\makebox[2cm][r]{\hfill$\varepsilon_i$}$< -\xi$')
# plt.plot(numpy.random.rand(10), label=r'\makebox[2cm][r]{\hfill$|\varepsilon_i|$}$< \xi$')
# plt.plot(numpy.random.rand(10), label=r'\makebox[24cm][r]{\hfill$\varepsilon_i$}$ > \xi$')

# the relations are aligned, but i do not want to plot the 'bla' for this
plt.plot(numpy.random.rand(10), label=r'\makebox[2cm][r]{bla\hfill$\varepsilon_i$}$< -\xi$')
plt.plot(numpy.random.rand(10), label=r'\makebox[2cm][r]{bla\hfill$|\varepsilon_i|$}$< \xi$')
plt.plot(numpy.random.rand(10), label=r'\makebox[2cm][r]{bla\hfill$\varepsilon_i$}$ > \xi$')
plt.legend(loc='upper right')
plt.show()

1 个答案:

答案 0 :(得分:2)

这是一个解决方案,其中LaTeX完美地对数学进行了算法,但是用户必须将痛苦置于图例中。想法是

  • 使用占位符
  • 在给定位置绘制图例框
  • 手动将amsmath array加入其中

以下是代码:

#!/usr/bin/python3

from numpy import arange

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

x = arange(5)
y = arange(5)

fig = plt.figure()
ax = fig.add_subplot(111)

l1, = ax.plot(x, y)
l2, = ax.plot(x * 2, y)
l3, = ax.plot(x * 3, y)

leg = ax.legend(
    [l1, l2, l3],
    ["", "", ""],
    bbox_to_anchor = (0.98, 0.25),
    handletextpad = 4,  # space between lines and text -- used here as a placeholder
    labelspacing = 0.1, # space between lines in a legend
    )
leg.set_zorder(1)

ax.text(0.955, 0.21,

    r"\begin{array}{rcl}"
    r"     \varepsilon_i & < & -\xi"
    r"\\ |\varepsilon_i| & < & \xi"
    r"\\   \varepsilon_i & > & \xi"
    r"\end{array}",

    transform = ax.transAxes,
    horizontalalignment = 'right',
    verticalalignment = 'top',
    zorder = 5,
        )

fig.savefig("mwe.png")

结果:

enter image description here

您可能希望将其编译两次:在第一次编译时,它可能会给您错误,但所有其他尝试都会正常。

对于图例中<符号之间的空格 - 可能会减少,例如:

ax.text(0.94, 0.21,

    r"\begin{array}{r@{}c@{}l}"
    r"     \varepsilon_i \,\,& < &\,\, -\xi"
    r"\\ |\varepsilon_i| \,\,& < &\,\, \xi"
    r"\\   \varepsilon_i \,\,& > &\,\, \xi"
    r"\end{array}",

(其他一切都一样)。这给出了:

enter image description here