Matplotlib 图例字符串格式化/对齐

发布于 2025-01-10 01:02:50 字数 583 浏览 1 评论 0原文

我正在尝试绘制一个带有格式良好的图例的图表。

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.

strong text

Could anyone please suggest a way to place this legend in such a way that all colons are aligned vertically ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我最亲爱的 2025-01-17 01:02:50

您可能想要将字体类型更改为等宽字体。

代码:

import matplotlib.pyplot as plt

test_label ="""\
CL      :1.2565
CM      :1.2565
Tot CD  :1.2565"""

# Change the font type.
plt.rcParams['font.family'] = 'monospace'

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)

输出:

在此处输入图像描述

You might want to change the font type into monospace.

Code:

import matplotlib.pyplot as plt

test_label ="""\
CL      :1.2565
CM      :1.2565
Tot CD  :1.2565"""

# Change the font type.
plt.rcParams['font.family'] = 'monospace'

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)

Output:

enter image description here

小帐篷 2025-01-17 01:02:50

您可以将图例的文本对齐方式设置为右侧,以便数字具有相同的位数并且可以对齐。

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)
legend = plt.legend(frameon=False)
for t in legend.get_texts():
    t.set_ha('right')
    
plt.show(block=False)

输入图片此处描述

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.

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)
legend = plt.legend(frameon=False)
for t in legend.get_texts():
    t.set_ha('right')
    
plt.show(block=False)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文