legend()显示图例
legend基础
函数原型 legend(*args, **kwargs)
当len(args)==2 args是[artist]和[label]的集合
当len(args)==0 args会自动调用get_legend_handles_labels()生成 等价于
handles, labels=ax.get_legend_handles_labels()
ax.legend(handles,labels)
ax.get_legend_handles_labels()的作用在于返回ax.lines, ax.patch所有对象以及ax.collection中的LineCollection or RegularPolyCollection 对象
注意:这里只提供有限支持,并不是所有的artist都可以用作图例,比如errorbar支持不完善
调整顺序
#!/usr/bin/env python
# coding=utf-8
import matplotlib.pyplot as plt
ax=plt.subplot(1,1,1)
p1,=ax.plot([1,2,3],label="line 1")
p2,=ax.plot([3,2,1],label="line 2")
p3,=ax.plot([2,3,1],label="line 3")
handles,labels=ax.get_legend_handles_labels()
#reverse the order
ax.legend(handles[::-1],labels[::-1])
plt.show()