# matplotlib 中的图例在对 bbox_to_anchor 参数进行最小更改的情况下跳跃

##### 问题描述投票：0回答：1

``loc``

``bbox_to_anchor``
。问题是，只要对
``x,y``

``bbox_to_anchor=(0.12,.9)``

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

x = np.arange(-2*np.pi, 2*np.pi, 0.1)
fig, ax = plt.subplots(1,1)
ax.plot(x, np.sin(x), label='Sine')
ax.plot(x, np.arctan(x), label='Inverse tan')
ax.legend(bbox_to_anchor=(0.12,.9))

plt.show()
``````

``bbox_to_anchor=(0.13,.9)``

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

x = np.arange(-2*np.pi, 2*np.pi, 0.1)
fig, ax = plt.subplots(1,1)
ax.plot(x, np.sin(x), label='Sine')
ax.plot(x, np.arctan(x), label='Inverse tan')
ax.legend(bbox_to_anchor=(0.13,.9))

plt.show()
``````

``bbox_to_anchor=(0.13,.8)``

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

x = np.arange(-2*np.pi, 2*np.pi, 0.1)
fig, ax = plt.subplots(1,1)
ax.plot(x, np.sin(x), label='Sine')
ax.plot(x, np.arctan(x), label='Inverse tan')
ax.legend(bbox_to_anchor=(0.13,.8))

plt.show()
``````

python matplotlib legend
##### 1个回答
0

``bbox_to_anchor``
，然后是图例的
``set_loc``
。因此，如果您不指定
``loc``
，则将使用默认值并将您的
``bbox_to_anchor``

``loc``

``best``
，它会等到绘制图形来计算最佳位置。由于您的图例已临时绘制在您的人物上，“最佳”位置的计算可能会返回一个新的
``loc``

``bbox_to_anchor``

``loc``

``loc``