关于 plt.legend() 中 bbox_to_anchor 的理解
loc

bbox_to_anchor 通俗解释
bbox_to_anchor 全称:bounding box to anchor
bbox:bounding box,包围盒(图例这个方框)anchor:锚点坐标
loc:规定图例自己的哪个点去对齐;bbox_to_anchor:规定要对齐到画布上哪个坐标点。
默认坐标系:axes 坐标系(子图坐标,不是数据坐标)
(0,0):子图左下角(1,1):子图右上角
例子 1:图例放到图外面右侧(最常用)
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 100) plt.plot(x, np.sin(x), label="sin(x)") plt.plot(x, np.cos(x), label="cos(x)") # loc="upper left":拿图例【左上角】 # bbox_to_anchor=(1.02, 1):对齐到子图坐标 (1.02,1) plt.legend(loc="upper left", bbox_to_anchor=(1.02, 1)) plt.tight_layout() plt.show()
含义: 拿图例的左上角,贴到子图右上角稍微靠右一点 (1.02,1),图例就跑到图外面右边。

例子 2:图例放到图上方中间
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 100) plt.plot(x, np.sin(x), label="sin(x)") plt.plot(x, np.cos(x), label="cos(x)") # loc="upper left":拿图例【左上角】 # bbox_to_anchor=(1.02, 1):对齐到子图坐标 (1.02,1) plt.legend(loc="lower center", bbox_to_anchor=(0.5, 1.05)) plt.tight_layout() plt.show()
plt.legend(loc="lower center", bbox_to_anchor=(0.5, 1.05))
loc="lower center":图例自身底边中点bbox_to_anchor=(0.5,1.05):对齐到子图横向中点、顶部往上一点 效果:图例悬浮在图的上方。

例子 3:对比只写 loc,不加 bbox_to_anchor
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 100) plt.plot(x, np.sin(x), label="sin(x)") plt.plot(x, np.cos(x), label="cos(x)") # loc="upper left":拿图例【左上角】 # bbox_to_anchor=(1.02, 1):对齐到子图坐标 (1.02,1) plt.legend(loc="upper right") plt.tight_layout() plt.show()
plt.legend(loc="upper right")
等价于:
plt.legend(loc="upper right", bbox_to_anchor=(1,1))
也就是:图例右上角对齐子图右上角,图例在图内部。

一句话
loc 选图例身上哪个点,bbox_to_anchor 选画布上的目标点,两点重合。
补充小坑
- 如果图例跑出画布被截断,配合
plt.tight_layout()或者subplots_adjust留出边距; - 可以用
bbox_transform切换坐标系(默认ax.transAxes子图相对坐标,也可以换成数据坐标)。
====================================
在 matplotlib 中,bbox_to_anchor 是用于调整图例(legend)、文本(text)等元素位置的重要参数,它的作用是指定元素的 “锚点框”(bounding box)相对于参考坐标系的位置。
参数形式
核心逻辑:与 loc 参数配合
坐标系规则
示例

width, height 两个参数实际和说明不一致。
================================================
https://blog.csdn.net/sinat_41299610/article/details/106494549
在用plt.legend()添加图例的时候,bbox_to_anchor()这个参数有时候是有两个元素,有时候是可以有四个元素,那么到底有什么区别?
1. 两个元素
对于两个元素的bbox_to_anchor(),也就是(x,y),这个参数是代表了lengend_box的起点,并且是有后面的loc决定 的。
首先明确,lengend_box是一个四边形,在这里为了方便理解将它的四条边成为:
左边,右边,顶边,底边
例如,
设置(0.5,0.5), loc='center',那么代表lengend_box的中心点(center)坐标是(0.5, 0.5)
设置(0.5,0.5), loc='lower center',那么代表底边的中点坐标是(0.5,0.5)
…
如图中标注的红点就是坐标(0.5,0.5)所在位置,然后根据loc参数的不同,对应到绿色的lengend_box位置也就不同。

2. 四个元素
对于四个元素的bbox_to_anchor(),也就是(x, y, width, height),情况就和上面两个元素的有所不同了。我们通过图来展示,会更清楚一点。

如图,红色框为bounding_box,绿色框为legend_box,在加上了(height, width)两个参数后,实际的中心(x=0.5, y=0.5)来到了红色框中,也就是图中黑色点标记处,然后再根据(height, width)分别扩展。
那个(height, width)对于绿色框又产生了什么影响呢?我们从图中的紫色点标记处来看,绿色框的起点应该是和loc参数对应。
当loc='center'时,绿色框的起点应该是在红色框的正中心,然后根据(height,width)向四周边扩散(因为时center);
当loc='lower center'时,绿色框的起点在红色框的底边中心,然后根据(height,width)向两边以及上面扩散(因为是lower+center);
…
同理可以类推出其他的图。
原文链接:https://blog.csdn.net/sinat_41299610/article/details/106494549
=======================================
import matplotlib.pyplot as plt import numpy as np import matplotlib.patches as patches x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) fig, ax = plt.subplots() ax.plot(x, y1, 'b-', label='sin(x)') ax.plot(x, y2, 'r--', label='cos(x)') # 设置图例的位置。loc定义基准位置,bbox_to_anchor进行微调。 ax.legend(loc='upper left', bbox_to_anchor=(1, 1)) plt.show()

=======================================
import matplotlib.pyplot as plt import numpy as np import matplotlib.patches as patches x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) fig, ax = plt.subplots() ax.plot(x, y1, 'b-', label='sin(x)') ax.plot(x, y2, 'r--', label='cos(x)') # 设置图例的位置。loc定义基准位置,bbox_to_anchor进行微调。 ax.legend(loc='upper left', bbox_to_anchor=(0, 0)) plt.show()

=======================================
import matplotlib.pyplot as plt import numpy as np import matplotlib.patches as patches x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) fig, ax = plt.subplots() ax.plot(x, y1, 'b-', label='sin(x)') ax.plot(x, y2, 'r--', label='cos(x)') # 设置图例的位置。loc定义基准位置,bbox_to_anchor进行微调。 ax.legend(loc='upper left', bbox_to_anchor=(0, 1)) plt.show()

=======================================
import matplotlib.pyplot as plt import numpy as np import matplotlib.patches as patches x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) fig, ax = plt.subplots() ax.plot(x, y1, 'b-', label='sin(x)') ax.plot(x, y2, 'r--', label='cos(x)') # 设置图例的位置。loc定义基准位置,bbox_to_anchor进行微调。 ax.legend(loc='upper left', bbox_to_anchor=(1, 0)) plt.show()

=======================================

浙公网安备 33010602011771号