gpt4 book ai didi

python - 在 pyplot 图例中添加内部空格

转载 作者:行者123 更新时间:2023-12-01 03:56:37 25 4
gpt4 key购买 nike

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

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

引号似乎是发布此内容所必需的,否则 SO 认为这是格式错误的代码。虚线代表线条。

我想让它左对齐,= 对齐,小数对齐。像这样的东西:

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

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

我认为我需要手动执行此操作,因此我尝试添加空格,我尝试将字符串与一个左对齐和另一个右对齐组合,但没有成功。

这可能吗?

谢谢

最佳答案

可以使用固定宽度字体,即等宽字体

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()

enter image description here

关于python - 在 pyplot 图例中添加内部空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37353663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com