Matplotlib 图例中的文本对齐

发布于 2024-12-12 09:17:27 字数 731 浏览 0 评论 0原文

我试图右对齐 matplotlib 轴图例中的条目(默认情况下它们是左对齐的),但似乎找不到任何方法来做到这一点。我的设置如下:(

我已使用 ax.plot() 命令将数据和标签添加到 my_fig 轴)

ax = my_fig.get_axes()[0]
legend_font = FontProperties(size=10)
ax.legend(prop=legend_font, num_points=1, markerscale=0.5)

matplotlib Axes,但似乎没有任何直接的方法来设置图例条目的对齐方式。有人知道这样做的后门方法吗?谢谢。

编辑:

为了澄清我想要实现的目标,现在我的传奇看起来像:

演习:2011 年 10 月 12 日 12:00 UTC 

偏差:2011 年 10 月 14 日 06:00 UTC

我希望它看起来像:

演习:2011 年 10 月 12 日 12:00 UTC 

    偏差:2011 年 10 月 14 日 06:00 UTC

I am trying to right-align the entries in a matplotlib axes legend (by default they are left-aligned), but can't seem to find any way of doing this. The setup I have is below:

(I have added data and labels to my_fig axes using the ax.plot() command)

ax = my_fig.get_axes()[0]
legend_font = FontProperties(size=10)
ax.legend(prop=legend_font, num_points=1, markerscale=0.5)

There is a list of legend keyword arguments in the docs for matplotlib Axes, but there doesn't seem to be any straighforward way to set the alignment of the legend entries there. Anybody know of a backdoor way of doing this? Thanks.

EDIT:

To clarify what I am trying to achieve, right now my legend looks like:

Maneuver: 12-OCT-2011 12:00 UTC 

Bias: 14-OCT-2011 06:00 UTC

I want it to look like:

Maneuver: 12-OCT-2011 12:00 UTC 

    Bias: 14-OCT-2011 06:00 UTC

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

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

发布评论

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

评论(4

无戏配角 2024-12-19 09:17:27

您要寻找的后门如下:

# get the width of your widest label, since every label will need 
# to shift by this amount after we align to the right
shift = max([t.get_window_extent().width for t in legend.get_texts()])
for t in legend.get_texts():
    t.set_ha('right') # ha is alias for horizontalalignment
    t.set_position((shift,0))

The backdoor you're looking for is the following:

# get the width of your widest label, since every label will need 
# to shift by this amount after we align to the right
shift = max([t.get_window_extent().width for t in legend.get_texts()])
for t in legend.get_texts():
    t.set_ha('right') # ha is alias for horizontalalignment
    t.set_position((shift,0))
阳光的暖冬 2024-12-19 09:17:27

我试图让这个例子工作,但我做不到。

至少从 matplotlib 版本 1.1.1(也许更早)开始,我们需要一个专用的渲染器实例。
照顾好定义渲染器的后端。根据后端的不同,输出可能在屏幕上看起来不错,但在 PDF 上看起来却很糟糕。

# get the width of your widest label, since every label will need 
#to shift by this amount after we align to the right
renderer = figure.canvas.get_renderer()
shift = max([t.get_window_extent(renderer).width for t in legend.get_texts()])
for t in legend.get_texts():
    t.set_ha('right') # ha is alias for horizontalalignment
    t.set_position((shift,0))

I tried to get the example work, but I couldn't.

At least since matplotlib version 1.1.1 (maybe earlier) we need a dedicated renderer instance.
Take care of your backend which defines the renderer. Depending on backend the output may look fine on screen but dismal as PDF.

# get the width of your widest label, since every label will need 
#to shift by this amount after we align to the right
renderer = figure.canvas.get_renderer()
shift = max([t.get_window_extent(renderer).width for t in legend.get_texts()])
for t in legend.get_texts():
    t.set_ha('right') # ha is alias for horizontalalignment
    t.set_position((shift,0))
爱殇璃 2024-12-19 09:17:27

@Paul Ivanov 的回答将我带向了正确的方向。但它对我来说需要稍微适应:

max_shift = max([t.get_window_extent().width for t in legend_obj.get_texts()])
for t in legend_obj.get_texts():
    t.set_ha('right')  # ha is alias for horizontalalignment
    temp_shift = max_shift - t.get_window_extent().width
    t.set_position((temp_shift, 0))

这种变化意味着我们根据每个对象自身的宽度和图例文本的最大宽度为每个对象设置不同的偏移。

对于出现 Cannot get windowextent w/o renderer 错误的用户,请添加 plt.pause(0.1) :)

The answer of @Paul Ivanov took me into the right direction. But it needed a slight adaptation for me:

max_shift = max([t.get_window_extent().width for t in legend_obj.get_texts()])
for t in legend_obj.get_texts():
    t.set_ha('right')  # ha is alias for horizontalalignment
    temp_shift = max_shift - t.get_window_extent().width
    t.set_position((temp_shift, 0))

The change means that we set a different shift for each object based on its own width and the max width of the legend text.

For those that get the Cannot get window extent w/o renderer error, add a plt.pause(0.1) :)

淡看悲欢离合 2024-12-19 09:17:27

这是 Paul Ivanov 解决方案的一个变体,13 年后为我工作。

我没有依赖 matplotlib 水平对齐,而是采用默认的左对齐并根据标签的宽度移动标签。

它本质上与 zwepp 在他们的解决方案中建议的相同。

legend_obj = fig.legends[0] # assuming the legend is attached to figure
shift = max([t.get_window_extent().width for t in legend_obj.get_texts()])
for t in legend_obj.get_texts():
    text_width = t.get_window_extent().width
    t.set_position((shift - text_width,0))

Here is a variation on the solution by Paul Ivanov that worked for me after 13 years.

Instead of relying on matplotlib horizontal alignment, I take the default left alignment and shift the labels according to their width.

It is essentially the same as what zwepp suggested in their solution.

legend_obj = fig.legends[0] # assuming the legend is attached to figure
shift = max([t.get_window_extent().width for t in legend_obj.get_texts()])
for t in legend_obj.get_texts():
    text_width = t.get_window_extent().width
    t.set_position((shift - text_width,0))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文