matplotlib 设置图例legend位置

 

loc参数

Location String Location Code
'best' 0
'upper right' 1
'upper left' 2
'lower left' 3
'lower right' 4
'right' 5
'center left' 6
'center right' 7
'lower center' 8
'upper center' 9
'center' 10

示例

from matplotlib import pyplot as plt

plt.plot([0, 1, 9], [3, 8, 2], label='asd')
plt.legend(loc=0)
plt.show()

 

bbox_to_anchor参数

参数值为一个元组,第一个数字代表横轴偏移量,第二个数字代表纵轴偏移量

示例

from matplotlib import pyplot as plt

plt.plot([0, 1, 9], [3, 8, 2], label='asd')
plt.legend(bbox_to_anchor=(0.2, 1.1))
plt.show()

 

posted @ 2023-01-21 12:13  小鱼圆又圆  阅读(166)  评论(0)    收藏  举报