matplotlib版本号:3.1.0
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
print(matplotlib.version) # 3.1.0
在使用matplotlib做图时,总免不了和图例(legend)打交道,那图例到底该放在哪?如何放到指定的位置?(本文只讨论legend的坐标系为axes的情况)
关于如何控制legend的位置,官网介绍了2个参数:loc 与 bbox_to_anchor 。
本篇文章看看loc如何控制legend的位置的,通过官网文档可以很容易得知loc参数有2种类型:
1.str类型,为我们准备了9种位置(可参考图1),示例:loc = 'upper left’
首先legend是一个bbox类型,是一个由4条边框围成的区域,轴域(axes)也是由4条边框围成的区域(x, y 轴,上边缘线,右边缘线),当loc为str类型时,表示 legend中str所代表的位置与轴域(axes)中str所代表的位置重合。
[‘upper left’ , ‘upper center’, ‘upper right’,
‘center left’, ‘center’, ‘center right’,
‘lower left’, ‘lower center’, ‘lower right’]