matplotlib-legend()

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()



















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章