简体   繁体   English

Matplotlib:如何垂直对齐多个图例

[英]Matplotlib: how to vertically align multiple legends

I have a plot which has three legends of different length and width. 我有一个情节,其中有三个长度和宽度不同的图例。 I'm trying to find a way to place them nicely onto the plot. 我正在尝试找到一种将它们很好地放置在情节上的方法。 Currently I'm placing them with the default loc= which is great for dealing with the vertical placement. 目前,我使用默认的loc=放置它们,这非常适合处理垂直放置。 The issue is that by default the legends are right aligned, which looks messy. 问题在于,默认情况下,图例是右对齐的,看起来很乱。

Is there a way to use the default loc= to place them on the plot, but to have them left aligned? 有没有一种方法可以使用默认的loc=将其放置在绘图上,但要使其对齐?

Example: From the legend guide . 示例:从图例指南中

import matplotlib.pyplot as plt

line1, = plt.plot([1,2,3], label="Line 1", linestyle='--')
line2, = plt.plot([3,2,1], label="Line 2\nThis is a \nvery long\nlegend", linewidth=4)
line3, = plt.plot([2,2,2], label="Can this be left justified?")

# Create a legend for the first two lines.
# 'loc' puts them in a nice place on the right.
first_legend = plt.legend(handles=[line1], loc=1)
second_legend = plt.legend(handles=[line2], loc=5)

# Add the legends manually to the current Axes.
ax = plt.gca().add_artist(first_legend)
ax = plt.gca().add_artist(second_legend)

# Create another legend for the last line.
plt.legend(handles=[line3], loc=4)

plt.show()

Which gives this 这给这个

三个传说情节

Now what I would really like is for the legends to left aligned but still on the right side of the plot. 现在,我真正想要的是使图例左对齐,但仍位于情节的右侧。 Like so: 像这样:

在此处输入图片说明

I know I can place them at a specific location but to do this I need to specify both the x and y coords, which will be fiddly since all 3 have variable heights and widths. 我知道我可以将它们放置在特定的位置,但是要做到这一点,我需要同时指定x和y坐标,这很奇怪,因为所有3个坐标的高度和宽度都不同。

You could use bbox_to_anchor to position legends precisely where you want them: 您可以使用bbox_to_anchor将图例精确定位在bbox_to_anchor位置:

fig, ax = plt.subplots()
line1, = ax.plot([1,2,3], label="Line 1", linestyle='--')
line2, = ax.plot([3,2,1], label="Line 2\nThis is a \nvery long\nlegend", linewidth=4)
line3, = ax.plot([2,2,2], label="Can this be left justified?")

# Create a legend for the first two lines.
# 'loc' sets anchor position of the legend frame relative to itself, 
# bbox_to_anchor puts legend's anchor to (x, y) in axes coordinates.
first_legend = ax.legend(handles=[line1], loc='upper left', bbox_to_anchor=(0.65, 1))
second_legend = ax.legend(handles=[line2], loc='center left', bbox_to_anchor=(0.65, 0.5))

# Add the legends manually to the current Axes.
ax.add_artist(first_legend)
ax.add_artist(second_legend)

# Create another legend for the last line.
ax.legend(handles=[line3], loc='lower left', bbox_to_anchor=(0.65, 0))

plt.show()

The only number you would need is x position of the legends' bbox_to_anchor to align to ( 0.65 in the example above). 您唯一需要的数字是图例的bbox_to_anchor x位置以对齐(在上面的示例中为0.65 )。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM