Python matplotlib 中的 bbox_to_anchor 和 loc

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25068384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:41:16  来源:igfitidea点击:

bbox_to_anchor and loc in matplotlib

pythonmatplotlib

提问by user3897208

I came across matplotlibcode which customizes legend location using keywords locand bbox_to_anchor. For example :

我遇到了matplotlib使用关键字locbbox_to_anchor. 例如 :

fig.legend([line1, line2], ['series1', 'series2'], bbox_to_anchor=[0.5, 0.5], 
           loc='center', ncol=2)

I have seen variation of above where bbox_to_anchoris used after loc.

我已经看到了上面的变体,bbox_to_anchorloc.

I understand the purpose of using bbox_to_anchorand locseparately. However, is there any benefit of using both in the same legend specification? From my understanding and usage, it appears to me that if bbox_to_anchoris specified, then the locparameter is pretty much don't care.

我理解使用bbox_to_anchorloc分开的目的。但是,在同一个图例规范中同时使用两者有什么好处吗?从我的理解和使用来看,在我看来,如果bbox_to_anchor指定了,那么loc参数几乎是无关紧要的。

Can anyone confirm this? I don't see any documentation regarding this.

任何人都可以证实这一点吗?我没有看到任何关于此的文档。

采纳答案by Gabriel

When bbox_to_anchorand locare used together, the locargument will inform matplotlib which part of the bounding box of the legend should be placed at the arguments of bbox_to_anchor. For example (I've simplified the command a bit), the three options below will produce different locations for your legend,

bbox_to_anchorloc一起使用时,loc参数将通知 matplotlib 图例的边界框的哪一部分应该放在 的参数处bbox_to_anchor。例如(我稍微简化了命令),下面的三个选项将为您的图例生成不同的位置,

 fig.legend([line1], ['series1'], bbox_to_anchor=[0.5, 0.5], loc='center')
 fig.legend([line1], ['series1'], bbox_to_anchor=[0.5, 0.5], loc='center left')
 fig.legend([line1], ['series1'], bbox_to_anchor=[0.5, 0.5], loc='center right')

The first command will put the center of the bounding box at axes coordinates 0.5,0.5. The second will put the center left edge of the bounding box at the same coordinates (i.e. shift the legend to the right). Finally, the third option will put the center right edge of the bounding box at the coordinates (i.e. shift the legend to the left).

第一个命令将边界框的中心放在轴坐标 0.5,0.5 处。第二个将把边界框的中心左边缘放在相同的坐标上(即向右移动图例)。最后,第三个选项将边界框的中心右边缘放在坐标处(即向左移动图例)。

回答by ImportanceOfBeingErnest

The explanation of @Gabriel is slightly misleading. bbox_to_anchor=[x0, y0]will create a bounding box with lower leftcorner at position [x0, y0]. The extend of the bounding box is zero - being equivalent to bbox_to_anchor=[x0, y0, 0, 0]. The legend will then be placed 'inside' this box and overlapp it according to the specified locparameter. So locspecifies where inside the box the legend sits.

@Gabriel 的解释有点误导。bbox_to_anchor=[x0, y0]将创建一个边界框左下角的位置角落[x0, y0]。边界框的扩展为零 - 等效于bbox_to_anchor=[x0, y0, 0, 0]。然后图例将放置在此框的“内部”并根据指定的loc参数重叠它。所以loc指定图例在框内的位置。

Also see this question What does a 4-element tuple argument for 'bbox_to_anchor' mean in matplotlib?

另请参阅此问题“bbox_to_anchor”的 4 元素元组参数在 matplotlib 中意味着什么?