将matplotlib中的图例行对齐

2024-05-15 21:32:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我用matplotlib绘制了一个绘图,并为此创建了一个图例(参见下面的代码)。我希望图例行水平对齐,以使关系>和{}对齐。为了适应类似问题的this和{a2}代码,我遇到了麻烦。在

我理解其基本思想:使用\makebox[width][alignment]{math expression before aligment}<math expression after alignment作为标签,这样epsilon表达式使用的空间总是使用相同的空间并向右对齐,因此左侧有空闲空间。在

但是链接中使用的\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()

Tags: the代码numpyplotmatplotlib空间pltrandom
1条回答
网友
1楼 · 发布于 2024-05-15 21:32:20

这里有一个解决方案,其中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

您可能需要编译它两次:在第一次编译时,它可能会给您错误,但所有其他的尝试都可以。在

至于图例中<符号之间的空格,可以用以下方式缩小:

^{pr2}$

(其他都一样)。这样可以得到:

enter image description here

相关问题 更多 >