matplotlib: legend()

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-22 23:40   122   0

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.5, 0.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 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP