如何定位和对齐matplotlib图形图例? [英] How to position and align a matplotlib figure legend?

查看:113
本文介绍了如何定位和对齐matplotlib图形图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个子图的图形,它们分别为2行和1列.我可以通过添加一个漂亮的人物图例

I have a figure with two subplots as 2 rows and 1 column. I can add a nice looking figure legend with

fig.legend((l1, l2), ['2011', '2012'], loc="lower center", 
           ncol=2, fancybox=True, shadow=True, prop={'size':'small'})

但是,此图例位于的中心,而不是我想要的的中心.现在,我可以使用

However, this legend is positioned at the center of the figure and not below the center of the axes as I would like to have it. Now, I can obtain my axes coordinates with

axbox = ax[1].get_position()

从理论上讲,我应该可以通过为元组指定 loc 关键字来定位图例:

and in theory I should be able to position the legend by specifying the loc keyword with a tuple:

fig.legend(..., loc=(axbox.x0+0.5*axbox.width, axbox.y0-0.08), ...)

这有效,除了,图例保持左对齐,以便 loc 指定图例框的左边缘/角而不是中心.我搜索了 align horizo​​ntalalignment 等关键字,但找不到任何关键字.我也尝试获取传奇位置",但是图例没有* get_position()*方法.我读到有关* bbox_to_anchor *的信息,但将其应用于人物图例时却无法理解.这似乎是为轴传奇而设计的.

This works, except that the legend is left aligned so that loc specifies the left edge/corner of the legend box and not the center. I searched for keywords such as align, horizontalalignment, etc., but couldn't find any. I also tried to obtain the "legend position", but legend doesn't have a *get_position()* method. I read about *bbox_to_anchor* but cannot make sense of it when applied to a figure legend. This seems to be made for axes legends.

或者:我应该改用轴图例吗?但是,为什么首先要有人物传说呢?而且,以某种方式必须可以使图形图例居中对齐",因为 loc ="lower center" 也是这样做的.

Or: should I use a shifted axes legend instead? But then, why are there figure legends in the first place? And somehow it must be possible to "center align" a figure legend, because loc="lower center" does it too.

感谢您的帮助,

马丁

推荐答案

在这种情况下,您可以将轴用于图形legend方法.无论哪种情况,bbox_to_anchor都是键.正如您已经注意到的,bbox_to_anchor指定一个坐标元组(或一个框),用于放置图例.使用bbox_to_anchor时,请考虑将location kwarg视为控制水平和垂直对齐方式.

In this case, you can either use axes for figure legend methods. In either case, bbox_to_anchor is the key. As you've already noticed bbox_to_anchor specifies a tuple of coordinates (or a box) to place the legend at. When you're using bbox_to_anchor think of the location kwarg as controlling the horizontal and vertical alignment.

区别只是坐标元组是解释为轴坐标还是图形坐标.

The difference is just whether the tuple of coordinates is interpreted as axes or figure coordinates.

作为使用图例的示例:

import numpy as np
import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)

x = np.linspace(0, np.pi, 100)

line1, = ax1.plot(x, np.cos(3*x), color='red')
line2, = ax2.plot(x, np.sin(4*x), color='green')

# The key to the position is bbox_to_anchor: Place it at x=0.5, y=0.5
# in figure coordinates.
# "center" is basically saying center horizontal alignment and 
# center vertical alignment in this case
fig.legend([line1, line2], ['yep', 'nope'], bbox_to_anchor=[0.5, 0.5], 
           loc='center', ncol=2)

plt.show()

作为使用axiss方法的示例,请尝试如下操作:

As an example of using the axes method, try something like this:

import numpy as np
import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)

x = np.linspace(0, np.pi, 100)

line1, = ax1.plot(x, np.cos(3*x), color='red')
line2, = ax2.plot(x, np.sin(4*x), color='green')

# The key to the position is bbox_to_anchor: Place it at x=0.5, y=0
# in axes coordinates.
# "upper center" is basically saying center horizontal alignment and 
# top vertical alignment in this case
ax1.legend([line1, line2], ['yep', 'nope'], bbox_to_anchor=[0.5, 0], 
           loc='upper center', ncol=2, borderaxespad=0.25)

plt.show()

这篇关于如何定位和对齐matplotlib图形图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆