Matplotlib 绘图之 Legend 图例
函数功能
Place a legend on the Axes.
函数调用方式
以下代码默认省略import numpy as np
和import matplotlib.pyplot as plt
不传入参数:legend
自动检测图例中显示的元素。在这种情况下,标签从artist中获取。在 artist 对象创建时指定或者通过 artist 对象的set_label()
方法指定。
1 | x = [1,2,3,4,5] |
对已有对象标记图例: legend(labels)
为已经存在的线添加图例,只需要为每个对象添加一个标签,此种方式不推荐使用,因为对象和标签之间的对应关系很容易混淆。
1 | x = np.arange(0, 5, 1) |
传入一一对象的 artist 对象与标签:legend(handles, labels)
1 | x = np.arange(0, 2.1, 0.1) |
函数参数
loc
参数 |
含义 |
---|---|
loc | 字符串或反应相对位置的浮点型数据组成的坐标数据;字符串的取值有多种,具体取值与展示效果如下所示 |
Location String | Location Code | 位置 |
---|---|---|
‘best’ (Axes only) | 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 | 中心 |
1 | import numpy as np |
当固定字符串的位置,无法满足要求,可以通过 loc 指定图例位置,此时的坐标位置为绘图区域 x 轴与 y 轴数值范围的比例,图例的左下角将放在此坐标位置。通过此参数可以实现在绘图区域内部与外部的图例展示。
图例的位置为二元数组,数组loc=(0,25,0.5)
,则图例左下角放置在横轴 25% 的位置,纵轴 50% 的位置,loc=(num,num)
内数值取值范围为[0,1]
。
1 | import numpy as np |
bbox_to_anchor
参数 | 含义 |
---|---|
… | … |
bbox_to_anchor | 两个或四个浮点数的元组,与 loc 参数一起决定图例的展示位置,具体如下: |
此参数与 loc 参数一起完成对图例的定位,图例参考区域当调用 axes.legend()时为绘图区域,当调用 figure.legend()时为画布区域,当 bbox_to_anchor 为 4 个浮点数组成的元组时,图例的参考区域为 bbox_to_anchor 确定的数据区域。
此时将loc
与bbox_to_anchor
同时使用,表明将图例的 loc 位置放置在bbox_to_anchor
确定的坐标点上,如:loc='center', bbox_to_anchor=(0.5,0.5)
,表示图例的中心位置在坐标系的中间位置。
1 | import numpy as np |
(x, y, width, height)
四个参数分别为图例所在参考区域的左下角的坐标位置(x,y)
,参考区域的宽度与高度(width, height)
。图例指定的loc
位置位于bbox_to_anchor
所确定参考区域的相应位置,如下所示:
1 | import numpy as np |
ncols
, fontsize
, labelcolor
参数 | 含义 |
---|---|
… | … |
ncols | 图例展示为几列,默认展示为 1 列,为兼容以前版本也可使用 ncol,但不推荐 |
fontsize | 图例的字体大小,整数或者字符串,取值为整数时,指定字体的大小(单位:磅);取值为字符串时,表示字体相对于默认字体(medium)的大小,字符串取值: {‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’} |
labelcolor | 图例中文本颜色,默认为黑色,取值可以为单个字符串颜色或颜色列表,也可以使用 ‘linecolor’ 或’markerfacecolor’ (or ‘mfc’), 或 ‘markeredgecolor’ (or ‘mec’)去匹配线条或者标记颜色 |
1 | import numpy as np |
文本颜色与线条颜色保持一致,使用下面的代码可以实现与指定颜色相同的效果:
1 | plt.legend( |
prop
参数 | 含义 |
---|---|
… | … |
prop | 图例的字体属性字典,当该参数存在时,fontsize 参数指定的字体大小将不被使用 |
1 | plt.legend( |
numpoints
, scatterpoints
参数 | 含义 |
---|---|
… | … |
numpoints | 绘制折线图时,图例中标记的数量,默认为 1 个,当绘制折线图标记时才能显示 |
scatterpoints | 绘制散点图时,图例中标记的数量,默认为 1 个 |
1 | import numpy as np |
markerscale
, markerfirst
参数 | 含义 |
---|---|
… | … |
markerscale | 图例中的标记相对于图中的原始大小 |
markerfirst | 布尔值,默认取值为 True,表示图例中的标记放在标签左边;取值为 False 表示图例标记放在标签右边 |
1 | import matplotlib.pyplot as plt |
frameon
, fancybox
, shadow
参数 | 含义 |
---|---|
… | … |
frameon | 布尔值,是否显示图例边框,默认取值为 True:显示 |
fancybox | 布尔值,是否绘制圆角边框,默认取值为 True:显示 |
shadow | 布尔值,是否显示图例的阴影效果,默认取值为 False:不显示 |
1 | import matplotlib.pyplot as plt |
facecolor
, edgecolor
参数 | 含义 |
---|---|
… | … |
facecolor | 图例填充颜色,默认为白色 |
edgecolor | 图例边界颜色,默认为白色 |
1 | import matplotlib.pyplot as plt |
mode
参数 | 含义 |
---|---|
… | … |
mode | 取值为 expand 或 None;当取值为 expand 时,图例将在水平方向扩展填充,填充到参考区域的最后边 |
1 | x = np.linspace(0, 10, 10) |
title
, title_fontproperties
参数 | 含义 |
---|---|
… | … |
title | 图例标题,默认无标题 |
title_fontproperties | 图例标题字体属性 |
title_fontsize | 取值为整数或 {‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’};图例字体大小,若同时需要设置其他字体属性,使用 title_fontproperties 属性;fontsize 与 title_propertiies 两个参数不用同时使用 |
1 | x = np.linspace(0, 10, 10) |
borderpad
参数 | 含义 |
---|---|
… | … |
borderpad | 图例距离边界框的距离,默认为 0.4 |
1 | import numpy as np |
labelspacing
参数 | 含义 |
---|---|
… | … |
lablespacing | 图例之间的垂直的距离,默认为 0.4 |
1 | import matplotlib.pyplot as plt |
handlelength
, handleheight
, handletextpad
参数 | 含义 |
---|---|
… | … |
handlelength | 图例的长度,默认为 2.0 |
handlelheight | 图例的高度,默认为 0.7 |
handleltextpad | 图例与图例文本之间的空白距离,默认为 0.8 |
1 | import matplotlib.pyplot as plt |
borderaxespad
, columnspacing
参数 | 含义 |
---|---|
… | … |
borderaxespad | 图例边框与指定位置之间的距离,默认为 0.5 |
columnspacing | 图例列间距,默认为 2.0 |
1 | import matplotlib.pyplot as plt |