在 Matplotlib 中,"bbox_to_anchor"的四元素元组参数是什么意思?
matplotlibserver side programmingprogramming更新于 2025/10/7 16:37:17
如果指定了四元组或 B 框 Base,则它指定了图例所在的 B 框 (x, y, width, height)。
步骤
- 使用 numpy 创建 x 和 y 数据点。
- 使用 plot() 方法绘制 x 和 y 坐标,标签为 y=sin(x),颜色为 green。
- 要将图例放置在特定位置,请使用位置"左上角",并使用图例框尺寸 以及上述描述中定义的四元组。
- 要显示图形,请使用 show() 方法。
示例
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-2, 2, 10) y = np.sin(x) plt.plot(x, y, c='green', label="y=sin(x)") plt.legend(loc='upper left', bbox_to_anchor=(0.05, 0.75, 0.2, 0.2)) plt.show()
输出


