Python学习之Matplotlib (二)

matplotlib.pyplot.legend


1、基本用法

import numpy as np
import matplotlib.pyplot as plt
x = [1,2,3,4,5]
plt.plot(x, label='label')

# 用法一
plt.legend() # 显示图例

# 用法二
plt.legend(['new label']) # 显示自定义内容的图例

2、设置图例位置

plt.legend(loc='best')

"""
参数loc可以选择下面的参数:
'best' 自动选取最佳位置
'upper right' 右上角
'upper left' 左上角
'lower left' 左下角
'lower right' 右下角
'right' 最右边
'center left' 最左边中间位置
'center right' 最右边中间位置
'lower center' 最下边中间位置
'upper center' 最上边中间位置
'center' 图正中间位置
"""

3、其他常用参数

'''
ncol 整数 图例的列数,默认值为1

fontsize 整型或浮点型,常用值{‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’}
         控制图例字体的大小
         
facecolor 控制图例的背景颜色,eg.legend(facecolor='r')

edgecolor 控制图例背景框边界颜色

title 设置图例标题
'''

4、 更多信息


猜你喜欢

转载自blog.csdn.net/liang_gu/article/details/78761932