numpy入门基础-可视化之图例
演示:
1、图例显示legend函数
在轴上放置图例
matplotlib.pyplot.legend(*args, **kwargs)
三种形式:(用到再自行百度吧)
legend()#讲这个
legend(labels)
legend(handles, labels)
2、示例
in:
import numpy as np #导入 numpyas
import matplotlib.pyplot as plt #导入 matplotlib.pyplot
#第一种显示图例的方式
x=np.arange(1,11,1)
plt.plot(x,x*2,label='Normal')
plt.plot(x,x*3,label='Fast')
plt.plot(x,x*4,label='Faster')
plt.legend()
out:
in:
#第二种显示图例的方式
x=np.arange(1,11,1)
plt.plot(x,x*2)
plt.plot(x,x*3)
plt.plot(x,x*4)
plt.legend(['Normal','Fast','Faster'])
out:
in:
#第三种显示图例的方式,比较麻烦
x=np.arange(1,100)
fig=plt.figure()
ax=fig.add_subplot(111)
s,=plt.plot(x,x*2)
s.set_label('label')
l,=plt.plot(x,x*4)
l.set_label('label2')
ax.legend()
out: