'bbox_to_anchor'的4元素元组参数在matplotlib中的含义是什么?

Dra*_*utX 24 python matplotlib

在matplotlib网站的"图例指南" 的"图例位置"部分中,有一个小脚本,其中第9行是plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=2, mode="expand", borderaxespad=0.).我见过的所有元组bbox_to_anchor都有2个元素,但是这个元素有4个.如果元组通过有4个元素,每个元素意味着什么?

我在pyplot.legend 文档中看到它,它说了一些关于bbox_transform坐标的东西.所以我环顾四周,发现matplotlib.transforms.Bbox了一个static from_bounds(x0, y0, width, height).

我猜测4元组参数的设置基于此from_bounds.我将脚本复制到Spyder,%matplotlib在Ipython控制台中完成,并更改了一些值.这似乎是有道理的,但当我尝试只改变.102类似的东西时0.9,传说并没有改变.我认为元组是基于的from_bounds,我只是不知道为什么改变元组中的最后一个值什么也没做.

Imp*_*est 32

你说得对,4元组中plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3)被设置为(x0, y0, width, height)在那里(x0,y0)有边框的左下角坐标.

当这些参数设置图例的边界框时,图例的实际垂直尺寸将缩小到放置元素所需的大小.此外,其位置仅与loc参数一起确定.loc参数设置边界框内图例的对齐方式,这样在某些情况下,更改时不会看到差异height,例如比较图(2)和(4).

在此输入图像描述

  • “宽度”和“高度”的单位是什么?我忍不住认为 `loc` 和 `bbox_to_anchor` 的使用过于复杂,特别是当它们的唯一目的是将 dang box 放置在屏幕上的某个位置时..(对咆哮表示歉意) (4认同)

jdh*_*hao 14

@ImportanceOfBeingErnest的答案很棒.我想扩展一下alignment图例框和边界框之间的含义.这意味着loc图例框和边界框的参数指示的不同位置将放在同一点.

例如,如果loc='center',图例框的中心和边界框将在同一点.如果loc='center right',图例框的中间右侧和边界框将在同一点.对不起有问题......

让我们举一个具体的例子来说明这个想法,

四个坐标 bbox_to_anchor

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as patches

locs = ['upper right', 'lower left', 'center left', 'lower center', 'center',
        'right']

x0, y0, width, height = 0.5, 0.5, 0.1, 0.4

x = np.arange(0.1, 4, 0.1)
y = 1.0/x

fig = plt.figure(figsize=(10, 10))

idx = 1
for i in range(0, 2):
    for j in range(0, 3):
        ax = fig.add_subplot(3, 2, idx)
        ax.plot(x, y, label=r'$\frac{1}{x}$')
        ax.legend(loc=locs[idx-1], bbox_to_anchor=(x0, y0, width, height),
            edgecolor='g', fontsize='large', framealpha=0.5,
            borderaxespad=0)
        ax.add_patch(
            patches.Rectangle((x0, y0), width, height, color='r',
                            fill=False, transform=ax.transAxes)
        )
        ax.text(0.6, 0.2, s="loc = '{}'".format(locs[idx-1]),
        transform=ax.transAxes)
        idx += 1

plt.show()
Run Code Online (Sandbox Code Playgroud)

上面的代码生成下面的图片, 在此输入图像描述

在图像中,红色框是边界框,绿色框是图例框.loc在每个子图中表示两个框之间的对齐关系.

两个坐标 bbox_to_anchor

当只给出两个值时bbox_to_anchor,边界框宽度和高度设置为零.我们稍微修改上面的代码,

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as patches

locs = ['upper right', 'lower left', 'center left', 'lower center', 'center',
        'right']

x0, y0, width, height = 0.5, 0.5, 0, 0

x = np.arange(0.1, 4, 0.1)
y = 1.0/x

fig = plt.figure(figsize=(10, 10))

idx = 1
for i in range(0, 2):
    for j in range(0, 3):
        ax = fig.add_subplot(3, 2, idx)
        ax.plot(x, y, label=r'$\frac{1}{x}$')
        ax.legend(loc=locs[idx-1], bbox_to_anchor=(x0, y0, width, height),
            edgecolor='g', fontsize='large', framealpha=0.5,
            borderaxespad=0)
        ax.add_patch(
            patches.Rectangle((x0, y0), width, height, color='r',
                            fill=False, transform=ax.transAxes)
        )
        ax.text(0.6, 0.2, s="loc = '{}'".format(locs[idx-1]),
        transform=ax.transAxes)
        ax.plot(x0, y0, 'r.', markersize=8, transform=ax.transAxes)
        idx += 1

plt.show()
Run Code Online (Sandbox Code Playgroud)

现在,产品图像变成了

在此输入图像描述

上图中的红点表示边界框坐标位置.