matplotlib: legend()

base knowledge:

    def legend(*args, **kwargs):

    ret = gca().legend(*args, **kwargs)
    return ret
#if (len(args)==2)  then the args mean the set of [artist] and [label]
# if (len(args==0)  then  it will equal handles,labels= ax.get_legend_handles_labels()
#                                        ax.legend(handles,labels)  #the sentence will get 
# from ax.labels,ax.patch and LinCollection or RegularPolyCollection
1.1 adjust the order    

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

ax.legend(handles[::-1],labels[::-1]) #reverse the order

 [转载]matplotlib学习笔记--Legend 
# sort them by labels
import operatorhl= sorted(zip(handles,labels),key=operator.itemgetter(1))
handles2,labels2=zip(*hl)
ax.legend(handles2,labels2)
[转载]matplotlib学习笔记--Legend
1.2 use agency artist
    if using legend doesn't support artist,we can use another artist which is supported by other legend
 for example:
    p=Rectangle((o,o),1,1,fc='r')
    legend([p],["Red Rectangle"])

2.multiseriate legend:

ax1 = plt.subplot(2,1,1)

ax1.plot([1], label="multinline")

ax1.plot([1], label="$2^{2^2}$")

ax1.plot([1], label=r"$frac{1}{2}pi$")

ax1.legend(loc=1, ncol=3, shadow=True)

 

ax2 = plt.subplot(2,1,2)

myplot(ax2)

ax2.legend(loc="center left", bbox_to_anchor=[0.50.5],

           ncol=2, shadow=True, title="Legend")

ax2.get_legend().get_title().set_color("red")

3. set location of legend
[转载]matplotlib学习笔记--Legend

[转载]matplotlib学习笔记--Legend
4.many legends

from matplotlib.pyplot import * p1, = plot([1,2,3], label="test1")

p2, = plot([3,2,1], label="test2")

l1 = legend([p1], ["Label 1"], loc=1)
l2 = legend([p2], ["Label 2"], loc=4) # this removes l1 from the axes.

gca().add_artist(l1) # add l1 as a separate artist to the axes

5.API

class matplotlib.legend.Legend(parent, handles, labels,**args)

parent ---

       比如用ax.legend()调用之后

       >>> print ax.get_legend().parent

       Axes(0.125,0.1;0.775x0.8)

handles --- artistlines, patches

labels --- labels of artist 

Other parameter:

Keyword

Description

loc

a location code

prop

the font property (matplotlib.font_manager.FontProperties 对象)

eg

song_font = matplotlib.font_manager.FontProperties(fname='simsun.ttc', size=8)

fontsize

the font size (prop互斥,不可同时使用)

markerscale

the relative size of legend markers vs. original

numpoints

the number of points in the legend for line

scatterpoints

the number of points in the legend for scatter plot

scatteryoffsets

a list of yoffsets for scatter symbols in legend

frameon

if True, draw a frame around the legend. If None, use rc

fancybox

if True, draw a frame with a round fancybox. If None, use rc

shadow

if True, draw a shadow behind legend

ncol

number of columns

borderpad

the fractional whitespace inside the legend border

labelspacing

the vertical space between the legend entries

handlelength

the length of the legend handles

handleheight

the length of the legend handles

handletextpad

the pad between the legend handle and text

borderaxespad

the pad between the axes and legend border

columnspacing

the spacing between columns

title

the legend title

bbox_to_anchor

the bbox that the legend will be anchored.

bbox_transform

the transform for the bbox. transAxes if None.

main fuction:

get_frame() ---  

get_lines()

get_patches()

get_texts()

get_title() ---  

set_bbox_to_anchor(bbox, transform=None)

Reference:

http://blog.sina.com.cn/s/blog_4b9acb520101an32.html

http://www.cnblogs.com/yinheyi/p/6792120.html


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值