matplotlib-legend()

来源:互联网 发布:lcd1602数据手册 pdf 编辑:程序博客网 时间:2024/05/22 15:23

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-8import matplotlib.pyplot as pltax=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 orderax.legend(handles[::-1],labels[::-1])plt.show()



















0 0
原创粉丝点击