Matplotlib 图例字符串格式化/对齐
我正在尝试绘制一个带有格式良好的图例的图表。
import matplotlib.pyplot as plt
test_label ="""\
CL :1.2565
CM :1.2565
Tot CD :1.2565"""
fig, ax = plt.subplots()
foil=[(0, 1), (0, 0), 'black']
ax.plot(*foil, label=test_label)
plt.xlim(-0.5, 1.5)
plt.ylim(-0.75, 0.75)
plt.legend(frameon=False)
plt.show(block=False)
正如您所看到的,test_label 的格式很好(垂直对齐的冒号),但是当我进行实际绘图时,这种对齐方式并未保留。
任何人都可以建议一种方法来放置此图例,使所有冒号垂直对齐吗?
I'm trying to plot a graph with a nicely formatted legend.
import matplotlib.pyplot as plt
test_label ="""\
CL :1.2565
CM :1.2565
Tot CD :1.2565"""
fig, ax = plt.subplots()
foil=[(0, 1), (0, 0), 'black']
ax.plot(*foil, label=test_label)
plt.xlim(-0.5, 1.5)
plt.ylim(-0.75, 0.75)
plt.legend(frameon=False)
plt.show(block=False)
As you can see the test_label is nicely formatted (vertically aligned colons), but this alignment is not preserved when I do the actual plot.
Could anyone please suggest a way to place this legend in such a way that all colons are aligned vertically ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想要将字体类型更改为等宽字体。
代码:
输出:
You might want to change the font type into
monospace
.Code:
Output:
您可以将图例的文本对齐方式设置为右侧,以便数字具有相同的位数并且可以对齐。
You can set the text justification of the legend to the right so that the numbers have the same number of digits and can be aligned.