在 pyplot 图例中添加内部空间

Adding internal spaces in pyplot legend

我想证明一个图例是左对齐和右对齐,并且等号对齐。例如我有以下代码:

import numpy as np
import matplotlib.pyplot as plt

KEu = 100.
KEm = 50.
KEl = 5.
X   = np.linspace(1,10,10) 

fig = plt.figure()
ax = plt.subplot(111)
ax.plot(X,KEu*X,label='Maximum Energy = {0:6.2f} MeV'.format(KEu))
ax.plot(X,KEm*X,label='Median Energy  = {0:6.2f} MeV'.format(KEm))
ax.plot(X,KEl*X,label='Minimum Energy = {0:6.2f} MeV'.format(KEl))
ax.legend(loc='upper left')
plt.show()

我不知道怎么加剧情所以下面写个例子:

-- Maximum Energy = 100.00 MeV  
-- Median Energy = 50.00 MeV     
-- Minimum Energy = 5.00 MeV  

引号似乎是 post 所必需的,否则 SO 认为它是格式不正确的代码。破折号代表线。

我想让它左对齐,= 对齐,小数点对齐。类似于:

-- Maximum Energy = 100.00 MeV
-- Median Energy  =  50.00 MeV  
-- Minimum Energy =   5.00 MeV

当然这并不完美,可能是我在情节中能做到的最好的,但我比原版更喜欢它。

我想我需要手动执行此操作,所以我尝试添加白色 space,我尝试将字符串与一个左对齐和另一个右对齐组合,但没有成功。

这可能吗?

谢谢

固定宽度的字体是可能的,即 monospace

import numpy as np
import matplotlib.pyplot as plt

KEu = 100.
KEm = 50.
KEl = 5.
X   = np.linspace(1,10,10) 

fig = plt.figure()
ax = plt.subplot(111)
ax.plot(X,KEu*X,label=r"Maximum Energy = {0:6.2f} MeV".format(KEu))
ax.plot(X,KEm*X,label=r"Median  Energy = {0:6.2f} MeV".format(KEm))
ax.plot(X,KEl*X,label=r"Minimum Energy = {0:6.2f} MeV".format(KEl))
ax.legend(loc='upper left', prop={'family': 'monospace'})
plt.show()