matplotlib.pyplot.legend()用来显示图例,legend基础语法及用法,官方文档
常用参数列表
loc:int或者str, 可选项如下表格,可使用左侧的字符串或者右侧的整型代号,这里'best'意味会自行调节位置以不遮挡内容,同时运行速度相对要慢。

frameon:设置图例的边框,默认为True, 设置为False时不显示边框
edgecolor:str, 可设置为'black','aqua',至于matplotlib中关于颜色详细列表可参考 matplotlib的颜色及线条控制
该文章的一张颜色图列出效果如下:

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0,10,200)#从0到10之间等距产生200个值
y1=np.sin(x)
y2=np.cos(x)
line1,=plt.plot(x,y1,linestyle='-.',label='sin(x)')#画出x在0-10之间时的sin(x)函数
legend1=plt.legend(handles=[line1],loc='upper right',frameon=False,fontsize=10)#定义图例位置,去除图例边框,定义图例文字大小
plt.gca().add_artist(legend1)#添加第一个的图例
line2,=plt.plot(x,y2,linestyle='-',label='cos(x)')#画出x在0-10之间时的cos(x)函数
plt.legend(handles=[line2],loc='best',edgecolor='aqua',fontsize=20)#定义图例位置,定义图例边缘颜色,定义图例文字大小
plt.show()#每次画完图别忘记显示

网友评论