函数功能

Place a legend on the Axes.

函数调用方式

以下代码默认省略import numpy as npimport matplotlib.pyplot as plt

不传入参数:legend

自动检测图例中显示的元素。在这种情况下,标签从artist中获取。在 artist 对象创建时指定或者通过 artist 对象的set_label()方法指定。

1
2
3
4
5
x = [1,2,3,4,5]
y = np.power(x,2)

plt.scatter(x,y,label='scatter')
plt.legend()

对已有对象标记图例: legend(labels)

为已经存在的线添加图例,只需要为每个对象添加一个标签,此种方式不推荐使用,因为对象和标签之间的对应关系很容易混淆。

1
2
3
4
5
6
7
x = np.arange(0, 5, 1)
y1 = np.array([1, 2, 3, 4, 5])
y2 = np.array([8, 7, 6, 5, 4])

plt.plot(x, y1)
plt.plot(x, y2)
plt.legend(['y1', 'y2'])

传入一一对象的 artist 对象与标签:legend(handles, labels)

1
2
3
4
5
6
7
8
9
10
11
x = np.arange(0, 2.1, 0.1)
y1 = np.power(x, 3)
y2 = np.power(x, 2)
y3 = np.power(x, 1)

# 此处的 line 后面必须加,才能正常输出图例内容,否则显示空白图例框(不明原因)
(line1,) = plt.plot(x, y1, ls=':')
(line2,) = plt.plot(x, y2, ls='--')
(line3,) = plt.plot(x, y3, ls='-')

plt.legend([line1, line2, line3], ['y=x^3', 'y=x^2', 'y=x'])

函数参数

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.arange(5, 0, -1)
y1 = np.array([32, 31, 28, 22, 18])

fig, ax = plt.subplots(3, 4, sharex=True, sharey=True)
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05, wspace=0, hspace=0)
ax[0, 0].plot(x, y1, marker='o', lw=1, label='upper left')
ax[0, 0].legend(loc='upper left')

ax[0, 1].plot(x, y1, marker='o', lw=1, label='upper right')
ax[0, 1].legend(loc='upper right')

ax[0, 2].plot(x, y1, marker='o', lw=1, label='lower left')
ax[0, 2].legend(loc='lower left')

ax[0, 3].plot(x, y1, marker='o', lw=1, label='lower right')
ax[0, 3].legend(loc='lower right')

ax[1, 0].plot(x, y1, marker='o', lw=1, label='upper center')
ax[1, 0].legend(loc='upper center')

ax[1, 1].plot(x, y1, marker='o', lw=1, label='lower center')
ax[1, 1].legend(loc='lower center')

ax[1, 2].plot(x, y1, marker='o', lw=1, label='center left')
ax[1, 2].legend(loc='center left')

ax[1, 3].plot(x, y1, marker='o', lw=1, label='center right')
ax[1, 3].legend(loc='center right')

ax[2, 0].plot(x, y1, marker='o', lw=1, label='center')
ax[2, 0].legend(loc='center')

ax[2, 1].plot(x, y1, marker='o', lw=1, label='best')
ax[2, 1].legend(loc='best')

ax[2, 2].plot(x, y1, marker='o', lw=1, label='默认位置')
ax[2, 2].legend()

ax[2, 3].plot(x, y1, marker='o', lw=1, label='right')
ax[2, 3].legend(loc='right')

plt.show()

当固定字符串的位置,无法满足要求,可以通过 loc 指定图例位置,此时的坐标位置为绘图区域 x 轴与 y 轴数值范围的比例,图例的左下角将放在此坐标位置。通过此参数可以实现在绘图区域内部与外部的图例展示。
图例的位置为二元数组,数组loc=(0,25,0.5),则图例左下角放置在横轴 25% 的位置,纵轴 50% 的位置,loc=(num,num)内数值取值范围为[0,1]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.arange(0, 16, 2)
y1 = np.arange(0, 8)

fig, ax = plt.subplots(1, 2, sharex=True, sharey=True)
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05, wspace=0.05, hspace=0)
ax[0].plot(x, y1, marker='o', lw=1, label='中间(0.5,0.5)')
ax[0].legend(loc=(0.5, 0.5))
ax[0].grid(ls=':', lw=1)
ax[0].set_ylim(0, 7)
ax[0].set_xlim(0, 14)

ax[1].plot(x, y1, marker='o', lw=1, label='图形外部')
ax[1].legend(loc=(0, 1))
ax[1].grid(ls=':', lw=1)

plt.show()

bbox_to_anchor

参数 含义
bbox_to_anchor 两个或四个浮点数的元组,与 loc 参数一起决定图例的展示位置,具体如下:

此参数与 loc 参数一起完成对图例的定位,图例参考区域当调用 axes.legend()时为绘图区域,当调用 figure.legend()时为画布区域,当 bbox_to_anchor 为 4 个浮点数组成的元组时,图例的参考区域为 bbox_to_anchor 确定的数据区域。

此时将locbbox_to_anchor同时使用,表明将图例的 loc 位置放置在bbox_to_anchor确定的坐标点上,如:loc='center', bbox_to_anchor=(0.5,0.5),表示图例的中心位置在坐标系的中间位置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.linspace(0, 10, 10)
y1 = np.linspace(0, 8, 10)

fig, ax = plt.subplots(1, 2, sharex=True, sharey=True)
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05, wspace=0.05, hspace=0)
ax[0].plot(x, y1, marker='o', lw=1, label='图例右上角在中间')
ax[0].legend(loc='upper right', bbox_to_anchor=(0.5, 0.5), borderaxespad=0)
ax[0].grid(ls=':', lw=1)
ax[0].set_ylim(0, 8)
ax[0].set_xlim(0, 10)
ax[0].set_xticks(ticks=np.arange(0, 11))

ax[1].plot(x, y1, marker='o', lw=1, label='图形左部中间在指定位置')
ax[1].legend(loc='center left', bbox_to_anchor=(7 / 10, 5 / 8))
ax[1].grid(ls=':', lw=1)

plt.show()

(x, y, width, height)四个参数分别为图例所在参考区域的左下角的坐标位置(x,y),参考区域的宽度与高度(width, height)。图例指定的loc位置位于bbox_to_anchor所确定参考区域的相应位置,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.linspace(0, 10, 10)
y1 = np.linspace(0, 8, 10)

fig, ax = plt.subplots(1, 2, sharex=True, sharey=True)
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05, wspace=0.05, hspace=0)
ax[0].plot(x, y1, marker='o', lw=1, label='框选区域右上角')
ax[0].legend(
loc='upper right', bbox_to_anchor=(5 / 10, 4 / 8, 3 / 10, 2 / 8), borderaxespad=0
)
ax[0].grid(ls=':', lw=1)
ax[0].set_ylim(0, 8)
ax[0].set_xlim(0, 10)
ax[0].set_xticks(ticks=np.arange(0, 11))

ax[1].plot(x, y1, marker='o', lw=1, label='框选区域左部中间')
ax[1].legend(loc='center left', bbox_to_anchor=(2 / 10, 3 / 8, 4 / 10, 4 / 8))
ax[1].grid(ls=':', lw=1)

plt.show()

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

y = np.arange(5, 0, -1)
d1 = np.array([32, 31, 28, 22, 18])
d2 = np.array([49, 51, 41, 38, 38])
d3 = np.array([19, 18, 31, 40, 44])
cont = ['热门话题', '微博热搜', '关注的大V、KOL', '超话社区', '垂直社区']
plt.barh(
y, d1, color='b', left=d2 + d3, tick_label=cont, label='参数互动(%)' # 默认从left=0开始
)
plt.barh(y, d2, left=d3, color='g', label='经常浏览(%)')
plt.barh(y, d3, left=0, color='y', label='较少或偶尔浏览(%)')

for i, j in zip(d3, y):
plt.text(i / 2, j, '%3.1f%%' % i)

for a, b, c in zip(d3, d2, y):
plt.text(a + b / 2, c, '%3.1f%%' % b)

for m, n, p, q in zip(d3, d2, d1, y):
plt.text(m + n + p / 2, q, '%3.1f%%' % p)

plt.title('微博端不同板块的浏览互动情况')

plt.xlim(0, 130)

# print(plt.ylim())
plt.ylim(0, 5.5)

plt.legend(loc='lower center', fontsize=10, ncol=3, labelcolor=['b', 'g', 'y'])
plt.show()

文本颜色与线条颜色保持一致,使用下面的代码可以实现与指定颜色相同的效果:

1
2
3
4
5
6
7
plt.legend(
loc='lower center',
ncol=3,
prop=dict(weight='bold', family='LiSu', size=8),
fontsize=16,
labelcolor='linecolor',
)

prop

参数 含义
prop 图例的字体属性字典,当该参数存在时,fontsize 参数指定的字体大小将不被使用
1
2
3
4
5
6
7
8
plt.legend(
loc='lower center',
ncol=3,
prop=dict(weight='bold', family='LiSu', size=8),
fontsize=16,
labelcolor=['b', 'g', 'y'],
)
plt.show()

numpoints, scatterpoints

参数 含义
numpoints 绘制折线图时,图例中标记的数量,默认为 1 个,当绘制折线图标记时才能显示
scatterpoints 绘制散点图时,图例中标记的数量,默认为 1 个
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.linspace(0, 10, 10)
y1 = np.linspace(0, 8, 10)

fig, ax = plt.subplots(2, 2, sharex=True, sharey=True)
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05, wspace=0.05, hspace=0)
ax[0, 0].plot(x, y1, marker='o', lw=1, label='显示标记点')
ax[0, 0].legend(
loc='upper right', bbox_to_anchor=(5 / 10, 4 / 8, 3 / 10, 2 / 8), numpoints=2
)
ax[0, 0].grid(ls=':', lw=1)
ax[0, 0].set_ylim(0, 8)
ax[0, 0].set_xlim(0, 10)
ax[0, 0].set_xticks(ticks=np.arange(0, 11))

ax[0, 1].plot(x, y1, lw=1, label='不显示标记点')
ax[0, 1].legend(
loc='center left', bbox_to_anchor=(2 / 10, 3 / 8, 4 / 10, 4 / 8), numpoints=2
)
ax[0, 1].grid(ls=':', lw=1)

ax[1, 0].scatter(x, y1, lw=1, label='显示标记点')
ax[1, 0].legend(
loc='center left', bbox_to_anchor=(2 / 10, 3 / 8, 4 / 10, 4 / 8), scatterpoints=2
)
ax[1, 0].grid(ls=':', lw=1)

ax[1, 1].scatter(x, y1, lw=1, label='默认情况')
ax[1, 1].legend(loc='center left', bbox_to_anchor=(2 / 10, 3 / 8, 4 / 10, 4 / 8))
ax[1, 1].grid(ls=':', lw=1)

plt.show()

markerscale, markerfirst

参数 含义
markerscale 图例中的标记相对于图中的原始大小
markerfirst 布尔值,默认取值为 True,表示图例中的标记放在标签左边;取值为 False 表示图例标记放在标签右边
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.linspace(0, 10, 10)
y1 = np.linspace(0, 8, 10)

fig, ax = plt.subplots(2, 2, sharex=True, sharey=True)
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05, wspace=0.0, hspace=0)
ax[0, 0].plot(x, y1, marker='o', lw=1, label='2倍--标记在前')
ax[0, 0].legend(
loc='upper right',
bbox_to_anchor=(5 / 10, 4 / 8, 3 / 10, 2 / 8),
numpoints=2,
markerscale=2,
markerfirst=True,
)
ax[0, 0].grid(ls=':', lw=1)
ax[0, 0].set_ylim(0, 8)
ax[0, 0].set_xlim(0, 10)
ax[0, 0].set_xticks(ticks=np.arange(0, 11))

ax[0, 1].plot(x, y1, lw=1, marker='o', label='1倍--标记在后')
ax[0, 1].legend(
loc='center left',
bbox_to_anchor=(2 / 10, 3 / 8, 4 / 10, 4 / 8),
numpoints=2,
markerscale=1,
markerfirst=False,
)
ax[0, 1].grid(ls=':', lw=1)

ax[1, 0].scatter(x, y1, lw=1, label='0.5倍--默认标记在前')
ax[1, 0].legend(
loc='center left',
bbox_to_anchor=(2 / 10, 3 / 8, 4 / 10, 4 / 8),
scatterpoints=2,
markerscale=0.5,
)
ax[1, 0].grid(ls=':', lw=1)

ax[1, 1].scatter(x, y1, lw=1, label='默认情况')
ax[1, 1].legend(loc='center left', bbox_to_anchor=(2 / 10, 3 / 8, 4 / 10, 4 / 8))
ax[1, 1].grid(ls=':', lw=1)

plt.show()

frameon, fancybox, shadow

参数 含义
frameon 布尔值,是否显示图例边框,默认取值为 True:显示
fancybox 布尔值,是否绘制圆角边框,默认取值为 True:显示
shadow 布尔值,是否显示图例的阴影效果,默认取值为 False:不显示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.linspace(0, 10, 10)
y1 = np.linspace(0, 8, 10)

fig, ax = plt.subplots(2, 2, sharex=True, sharey=True)
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05, wspace=0.0, hspace=0)
ax[0, 0].plot(x, y1, marker='o', lw=1, label='默认情况')
ax[0, 0].legend(
loc='upper right',
bbox_to_anchor=(5 / 10, 4 / 8, 3 / 10, 2 / 8),
)
ax[0, 0].grid(ls=':', lw=1)
ax[0, 0].set_ylim(0, 8)
ax[0, 0].set_xlim(0, 10)
ax[0, 0].set_xticks(ticks=np.arange(0, 11))

ax[0, 1].plot(x, y1, lw=1, marker='o', label='不显示图例边框')
ax[0, 1].legend(
loc='center left', bbox_to_anchor=(2 / 10, 3 / 8, 4 / 10, 4 / 8), frameon=False
)
ax[0, 1].grid(ls=':', lw=1)

ax[1, 0].scatter(x, y1, lw=1, label='直角边框')
ax[1, 0].legend(
loc='center left', bbox_to_anchor=(2 / 10, 3 / 8, 4 / 10, 4 / 8), fancybox=False
)
ax[1, 0].grid(ls=':', lw=1)

ax[1, 1].scatter(x, y1, lw=1, label='阴影效果')
ax[1, 1].legend(
loc='center left', bbox_to_anchor=(2 / 10, 3 / 8, 4 / 10, 4 / 8), shadow=True
)
ax[1, 1].grid(ls=':', lw=1)

plt.show()

facecolor, edgecolor

参数 含义
facecolor 图例填充颜色,默认为白色
edgecolor 图例边界颜色,默认为白色
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.linspace(0, 10, 10)
y1 = np.linspace(0, 8, 10)

fig, ax = plt.subplots(2, 2, sharex=True, sharey=True)
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05, wspace=0.0, hspace=0)
ax[0, 0].plot(x, y1, marker='o', lw=1, label='默认情况')
ax[0, 0].legend(
loc='upper right', bbox_to_anchor=(5 / 10, 4 / 8, 3 / 10, 2 / 8), facecolor='y'
)
ax[0, 0].grid(ls=':', lw=1)
ax[0, 0].set_ylim(0, 8)
ax[0, 0].set_xlim(0, 10)
ax[0, 0].set_xticks(ticks=np.arange(0, 11))

ax[0, 1].plot(x, y1, lw=1, marker='o', label='不显示图例边框')
ax[0, 1].legend(
loc='center left', bbox_to_anchor=(2 / 10, 3 / 8, 4 / 10, 4 / 8), edgecolor='r'
)
ax[0, 1].grid(ls=':', lw=1)

ax[1, 0].scatter(x, y1, lw=1, label='直角边框')
ax[1, 0].legend(
loc='center left',
bbox_to_anchor=(2 / 10, 3 / 8, 4 / 10, 4 / 8),
facecolor='y',
edgecolor='r',
)
ax[1, 0].grid(ls=':', lw=1)

ax[1, 1].scatter(x, y1, lw=1, label='阴影效果')
ax[1, 1].legend(
loc='center left',
bbox_to_anchor=(2 / 10, 3 / 8, 4 / 10, 4 / 8),
)
ax[1, 1].grid(ls=':', lw=1)

plt.show()

mode

参数 含义
mode 取值为 expand 或 None;当取值为 expand 时,图例将在水平方向扩展填充,填充到参考区域的最后边
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
x = np.linspace(0, 10, 10)
y1 = np.linspace(0, 8, 10)

fig, ax = plt.subplots(2, 2, sharex=True, sharey=True)
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05, wspace=0.0, hspace=0)
ax[0, 0].plot(x, y1, marker='o', lw=1, label='默认情况')
ax[0, 0].legend(
loc='upper right', bbox_to_anchor=(5 / 10, 4 / 8, 3 / 10, 2 / 8), mode='expand'
)
ax[0, 0].grid(ls=':', lw=1)
ax[0, 0].set_ylim(0, 8)
ax[0, 0].set_xlim(0, 10)
ax[0, 0].set_xticks(ticks=np.arange(0, 11))

ax[0, 1].plot(x, y1, lw=1, marker='o', label='不显示图例边框')
ax[0, 1].legend(loc='upper left', bbox_to_anchor=(2 / 10, 3 / 8), mode='expand')
ax[0, 1].grid(ls=':', lw=1)

ax[1, 0].scatter(x, y1, lw=1, label='直角边框')
ax[1, 0].legend(loc=(4 / 10, 4 / 8), mode='expand')
ax[1, 0].grid(ls=':', lw=1)

ax[1, 1].scatter(x, y1, lw=1, label='阴影效果')
ax[1, 1].legend(loc='center left', mode='expand')
ax[1, 1].grid(ls=':', lw=1)

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
x = np.linspace(0, 10, 10)
y1 = np.linspace(0, 8, 10)

fig, ax = plt.subplots(2, 2, sharex=True, sharey=True)
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05, wspace=0.0, hspace=0)
ax[0, 0].plot(x, y1, marker='o', lw=1, label='图例标题')
ax[0, 0].legend(
loc='upper right',
bbox_to_anchor=(5 / 10, 4 / 8, 3 / 10, 2 / 8),
title='仅文字大小',
title_fontsize=10,
)
ax[0, 0].grid(ls=':', lw=1)
ax[0, 0].set_ylim(0, 8)
ax[0, 0].set_xlim(0, 10)
ax[0, 0].set_xticks(ticks=np.arange(0, 11))

ax[0, 1].plot(x, y1, lw=1, marker='o', label='图例标题')
ax[0, 1].legend(
loc='upper left',
bbox_to_anchor=(2 / 10, 3 / 8),
title='仅文字大小',
title_fontproperties=dict(size=10),
)
ax[0, 1].grid(ls=':', lw=1)

ax[1, 0].scatter(x, y1, lw=1, label='图例标题')
ax[1, 0].legend(
loc=(4 / 10, 4 / 8),
title='文字大小&粗细',
title_fontproperties=dict(size=10, weight='bold'),
)
ax[1, 0].grid(ls=':', lw=1)

ax[1, 1].scatter(x, y1, lw=1, label='图例标题')
ax[1, 1].legend(
loc='center left',
)
ax[1, 1].grid(ls=':', lw=1)

borderpad

参数 含义
borderpad 图例距离边界框的距离,默认为 0.4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.linspace(0, 10, 10)
y1 = np.linspace(0, 8, 10)

fig, ax = plt.subplots(2, 2, sharex=True, sharey=True)
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05, wspace=0.05, hspace=0)
ax[0, 0].plot(x, y1, marker='o', lw=1, label='默认值0.4')
ax[0, 0].legend(
loc='upper right', bbox_to_anchor=(5 / 10, 4 / 8, 3 / 10, 2 / 8), borderpad=0.4
)
ax[0, 0].grid(ls=':', lw=1)
ax[0, 0].set_ylim(0, 8)
ax[0, 0].set_xlim(0, 10)
ax[0, 0].set_xticks(ticks=np.arange(0, 11))

ax[0, 1].plot(x, y1, lw=1, marker='o', label='borderpad=0.8')
ax[0, 1].legend(loc='upper left', bbox_to_anchor=(2 / 10, 3 / 8), borderpad=0.8)
ax[0, 1].grid(ls=':', lw=1)

ax[1, 0].scatter(x, y1, lw=1, label='borderpad=0.0')
ax[1, 0].legend(loc=(4 / 10, 4 / 8), borderpad=0.0)
ax[1, 0].grid(ls=':', lw=1)

ax[1, 1].scatter(x, y1, lw=1, label='borderpad=2')
ax[1, 1].legend(loc='center left', borderpad=2)
ax[1, 1].grid(ls=':', lw=1)

plt.show()

labelspacing

参数 含义
lablespacing 图例之间的垂直的距离,默认为 0.4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.linspace(0, 10, 10)
y1 = np.linspace(0, 8, 10)
y2 = np.power(x, 0.5)
fig, ax = plt.subplots(2, 2, sharex=True, sharey=True)
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05, wspace=0.05, hspace=0)
ax[0, 0].plot(x, y1, marker='o', lw=1, label='labelspacing=0.5')
ax[0, 0].plot(x, y2, marker='^', lw=1, label='labelspacing=0.5')
ax[0, 0].legend(
loc='upper right', bbox_to_anchor=(5 / 10, 4 / 8, 3 / 10, 2 / 8), labelspacing=0.5
)
ax[0, 0].grid(ls=':', lw=1)
ax[0, 0].set_ylim(0, 8)
ax[0, 0].set_xlim(0, 10)
ax[0, 0].set_xticks(ticks=np.arange(0, 11))

ax[0, 1].plot(x, y1, lw=1, marker='o', label='labelspacing=0.8')
ax[0, 1].plot(x, y2, lw=1, marker='^', label='labelspacing=0.8')
ax[0, 1].legend(loc='upper left', bbox_to_anchor=(2 / 10, 3 / 8), labelspacing=0.8)
ax[0, 1].grid(ls=':', lw=1)

ax[1, 0].scatter(x, y1, lw=1, label='labelspacing=0.0')
ax[1, 0].plot(x, y1, lw=1, marker='o', label='labelspacing=0.0')
ax[1, 0].legend(loc=(4 / 10, 4 / 8), labelspacing=0.0)
ax[1, 0].grid(ls=':', lw=1)

ax[1, 1].scatter(x, y1, lw=1, label='labelspacing=2')
ax[1, 1].plot(x, y1, lw=1, marker='o', label='labelspacing=2')
ax[1, 1].legend(loc='center left', labelspacing=2)
ax[1, 1].grid(ls=':', lw=1)

plt.show()

handlelength, handleheight, handletextpad

参数 含义
handlelength 图例的长度,默认为 2.0
handlelheight 图例的高度,默认为 0.7
handleltextpad 图例与图例文本之间的空白距离,默认为 0.8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.linspace(0, 10, 10)
y1 = np.linspace(0, 8, 10)
y2 = np.power(x, 0.5)
fig, ax = plt.subplots(2, 2, sharex=True, sharey=True)
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05, wspace=0.05, hspace=0)
ax[0, 0].plot(x, y1, marker='o', lw=1, label='均为默认值')
# ax[0, 0].plot(x, y2, marker='^', lw=1, label='labelspacing=0.5')
ax[0, 0].legend(
loc='upper right',
bbox_to_anchor=(5 / 10, 4 / 8, 3 / 10, 2 / 8),
handlelength=2.0,
handleheight=0.7,
handletextpad=0.8,
)
ax[0, 0].grid(ls=':', lw=1)
ax[0, 0].set_ylim(0, 8)
ax[0, 0].set_xlim(0, 10)
ax[0, 0].set_xticks(ticks=np.arange(0, 11))

ax[0, 1].plot(x, y1, lw=1, marker='o', label='改变handlelength')
# ax[0, 1].plot(x, y2, lw=1, marker='^', label='labelspacing=0.8')
ax[0, 1].legend(
loc='upper left',
bbox_to_anchor=(2 / 10, 3 / 8),
handlelength=6.0,
handleheight=0.7,
handletextpad=0.8,
)
ax[0, 1].grid(ls=':', lw=1)

ax[1, 0].scatter(x, y1, lw=1, label='改变handleheight')
# ax[1, 0].plot(x, y1, lw=1, marker='o', label='labelspacing=0.0')
ax[1, 0].legend(
loc=(4 / 10, 4 / 8), handlelength=2.0, handleheight=2, handletextpad=0.8
)
ax[1, 0].grid(ls=':', lw=1)

ax[1, 1].scatter(x, y1, lw=1, label='改变handletextpad')
# ax[1, 1].plot(x, y1, lw=1, marker='o', label='labelspacing=2')
ax[1, 1].legend(loc='center left', handlelength=2.0, handleheight=0.7, handletextpad=3)
ax[1, 1].grid(ls=':', lw=1)

plt.show()

borderaxespad, columnspacing

参数 含义
borderaxespad 图例边框与指定位置之间的距离,默认为 0.5
columnspacing 图例列间距,默认为 2.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.linspace(0, 10, 10)
y1 = np.linspace(0, 8, 10)
y2 = np.power(x, 0.5)
fig, ax = plt.subplots(2, 2, sharex=True, sharey=True)
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05, wspace=0.05, hspace=0)
ax[0, 0].plot(x, y1, marker='o', lw=1, label='均为默认值')
ax[0, 0].plot(x, y2, marker='^', lw=1, label='均为默认值')
ax[0, 0].legend(
loc='upper right',
bbox_to_anchor=(5 / 10, 4 / 8, 3 / 10, 2 / 8),
ncol=2,
borderaxespad=0.5,
columnspacing=2.0,
)
ax[0, 0].grid(ls=':', lw=1)
ax[0, 0].set_ylim(0, 8)
ax[0, 0].set_xlim(0, 10)
ax[0, 0].set_xticks(ticks=np.arange(0, 11))

ax[0, 1].plot(x, y1, lw=1, marker='o', label='改变borderaxespad')
ax[0, 1].plot(x, y2, lw=1, marker='^', label='改变lborderaxespad')
ax[0, 1].legend(
loc='upper left',
bbox_to_anchor=(2 / 10, 3 / 8),
ncol=2,
borderaxespad=2,
columnspacing=2.0,
)
ax[0, 1].grid(ls=':', lw=1)

ax[1, 0].scatter(x, y1, lw=1, label='改变columnspacing')
ax[1, 0].plot(x, y1, lw=1, marker='o', label='改变columnspacing')
ax[1, 0].legend(
loc='center left',
ncol=2,
borderaxespad=0.5,
columnspacing=6.0,
)
ax[1, 0].grid(ls=':', lw=1)

ax[1, 1].scatter(x, y1, lw=1, label='borderaxespad=0.0')
ax[1, 1].plot(x, y1, lw=1, marker='o', label='borderaxespad=0.0')
ax[1, 1].legend(
loc='upper right',
bbox_to_anchor=(5 / 10, 4 / 8, 3 / 10, 2 / 8),
ncol=2,
borderaxespad=0.0,
columnspacing=2.0,
)
ax[1, 1].grid(ls=':', lw=1)

plt.show()