Common parameters of matplotlib fig.legend() include position adjustment and font settings, etc.

1. Four methods

legend()
legend(handles, labels)
legend(handles=handles)
legend(labels)

1 legend()

Labels are automatically obtained through drawing ( Automatic detection of elements to be shown in the legend )

# 第一种方法
ax.plot([1, 2, 3], label='Inline label')
ax.legend()
# 第二种方法
line, = ax.plot([1, 2, 3])
line.set_label('Label via method')
ax.legend()

2 legend(handles, labels)

明确列出(Explicitly listing the artists and labels in the legend

ax.legend([line1, line2, line3], ['label1', 'label2', 'label3'])

3 legend(handles=handles)

只列出handlesExplicitly listing the artists in the legend

line1, = ax.plot([1, 2, 3], label='label1')
line2, = ax.plot([1, 2, 3], label='label2')
ax.legend(handles=[line1, line2])

4 legend(labels)

List labels only , not recommended, because the relationship between plot elements and labels is only implied by their order, and can be easily confused.

ax.plot([1, 2, 3])
ax.plot([5, 6, 7])
ax.legend(['First line', 'Second line'])

2. Important parameters

1 handlessequence of Artist, optional

The plot element to add to the legend.

A list of Artists (lines, patches) to be added to the legend.

2 labels list of str, optional

Labels next to plot elements.

A list of labels to show next to the artists.

3 loc (default: 'best'**)

Control the position of the legend (when setting the position of the legend, set this parameter first!!!)

insert image description here
insert image description here

4 bbox_to_anchor

2-tuple or 4-tuple. Generally, we use 2-tuple
to loccontrol the position box of the legend used together. This parameter can adjust any position of the legend.
**Note: use bbox_to_anchorwith and !loc

If it is 4-tuple , respectively represent(x, y, width, height)

x represents the horizontal axis, x=0 is the leftmost

y represents the y-axis, y=0 is the bottom side

width means width

height means height

one example:

# 
loc='upper right', bbox_to_anchor=(0.5, 0.0, 0.5, 0.5)

If it is 2-tuple , respectively represent(x, y)

Note: When using loc=(x,y), x and y are not the actual values ​​of x and y in the axis field, but the x-axis and y-axis are regarded as 1,

Namely: (x/(x_max-x_min), y/(y_max-y_min)) (normalized to 1);

The bigger the x, the more right

The larger the y, the more upward

one example:

# 如果不设置loc,默认将图例的【左下角】放在图的(0.5, 0.5)
bbox_to_anchor=(0.5, 0.5)

# 如果设置了loc
# 将图例的右上角点放在图的(0.5, 0.5)的位置【这个很重要】
loc='upper right', bbox_to_anchor=(0.5, 0.5)
# 将图例的右上角点放在图的(0.5, 0.5)【这个很重要】
loc='upper left', bbox_to_anchor=(0.5, 0.5)

5 prop None or or dictFontProperties

To set the font properties of the legend, it is generally most convenient to use a dictionary

prop={‘weight’: ‘bold’, ‘size’: 12}

6 ncols int, default: 1

the number of columns of the legend

7 markerfirst bool, default: True

If True, the legend marker will be placed to the left of the legend label.

If False, the legend marker will be placed to the right of the legend label.

8 frameon default: True

whether outside the legend frame

9 mode {“expand”, None}

If 'expand', the legend will expand horizontally to fill the axes area (or bbox_to_anchor if defining the size of the legend).

Summarize

1 Multiple subgraphs share a legend usingfig.legend()

2 Multiple subgraphs [use a legend for each graph] useax.legend()

3 position big callloc

4 Use **bbox_to_anchor** the position of the legend to be set at will [it can be inside or outside the picture]

Learning link:

Guess you like

Origin blog.csdn.net/weixin_45913084/article/details/132218618