Legend图例:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3,3,50)
y1 = 2*x+1
y2 = x**2
plt.figure()
plt.xlim((-1,2))
plt.ylim((-2,3))
plt.xlabel("I am x")
plt.ylabel("I am y")
new_ticks = np.linspace(-1,2,5)
plt.xticks(new_ticks)
plt.yticks([-2,-1.8,-1,1.22,3],[r"$really\ bad$",r"$bad$",r"$normal$",r"$goog$",r"$really\ goog$"])
plt.plot(x,y2,label='up')
plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--',label='down')
plt.legend()
plt.show()
结果:
对legend做修改:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3,3,50)
y1 = 2*x+1
y2 = x**2
plt.figure()
plt.xlim((-1,2))
plt.ylim((-2,3))
plt.xlabel("I am x")
plt.ylabel("I am y")
new_ticks = np.linspace(-1,2,5)
plt.xticks(new_ticks)
plt.yticks([-2,-1.8,-1,1.22,3],[r"$really\ bad$",r"$bad$",r"$normal$",r"$goog$",r"$really\ goog$"])
l1, = plt.plot(x,y2,label='up')
l2, = plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--',label='down')
plt.legend(handles=[l1,l2,],labels=['aaa','bbb'],loc='best')
plt.show()
其中
,legend方法中的labels名称将会覆盖掉前面定义的up和down;loc = 'best' 是指在图像中找到合适的位置放置legend;
结果如下: