我用matplotlib绘制了一个绘图,并为此创建了一个图例(参见下面的代码)。我希望图例行水平对齐,以使关系>
和{
我理解其基本思想:使用\makebox[width][alignment]{math expression before aligment}<math expression after alignment
作为标签,这样epsilon表达式使用的空间总是使用相同的空间并向右对齐,因此左侧有空闲空间。在
但是链接中使用的\hfill
-方法只有在hfill前面有文本或者对齐方式是标准的(左)时才有效。解决方案必须非常接近,任何帮助将不胜感激。
文字的传说应该是怎样的
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()
这里有一个解决方案,其中LaTeX完美地协调了数学,但用户必须忍痛将其定位在图例中。我的想法是
array
放入其中代码如下:
结果:
您可能需要编译它两次:在第一次编译时,它可能会给您错误,但所有其他的尝试都可以。在
至于图例中
^{pr2}$<
符号之间的空格,可以用以下方式缩小:(其他都一样)。这样可以得到:
相关问题 更多 >
编程相关推荐