Axes.legend¶
- Axes.legend(handles=None, labels=None, *, loc=None, location=None, queue=False, **kwargs)[source]¶
Add an inset legend or outer legend along the edge of the axes.
- Parameters
handles (
listofArtist, optional) – List of matplotlib artists, or a list of lists of artist instances (see thecenterkeyword). IfNone, the artists are retrieved automatically. If the object is aCollectionor aContourSet, thelegend_elementsmethod is used to pair the collection or contour set label with the central element in the list (generally giving the central colormap color if the object is controlled with a colormap).labels (
listofstr, optional) – A matching list of string labels orNoneplaceholders, or a matching list of lists (see thecenterkeyword). WhereverNoneappears in the list (or if no labels were passed at all), labels are retrieved by callingget_labelon eachArtistin the handle list. If a handle consists of a tuple group of artists, labels are inferred from the artists in the tuple. If there are multiple unique labels in the tuple group of artists, the tuple group is expanded into unique legend entries. Otherwise, the tuple group elements are drawn on top of eachother. For details on matplotlib’s legend handlers, including tuple groups, see the matplotlib legend guide.loc, location (
intorstr, optional) – The legend location. Default isrc['legend.loc']='best'. Valid location keys are shown in the below table.Location
Valid keys
outer left
'left','l'outer right
'right','r'outer bottom
'bottom','b'outer top
'top','t'“best” inset
'best','inset','i',0upper right inset
'upper right','ur',1upper left inset
'upper left','ul',2lower left inset
'lower left','ll',3lower right inset
'lower right','lr',4center left inset
'center left','cl',5center right inset
'center right','cr',6lower center inset
'lower center','lc',7upper center inset
'upper center','uc',8center inset
'center','c',9“filled”
'fill'width (
floatorstr, optional) – For outer legends only. The space allocated for the legend box. This does nothing if the tight layout algorithm is active for the figure. If float, units are inches. If string, interpreted byunits.space (
floatorstr, optional) – For outer legends only. The fixed space between the legend and the main axes. If float, units are em-widths. If string, interpreted byunits. When the tight layout algorithm is active for the figure, this is adjusted automatically usingpad. Otherwise, a suitable default is selected.pad (
floatorstr, optional) – The padding between the axes edge and the legend. For outer legends, this is the tight layout padding. Default isrc['subplots.panelpad']=0.5. For inset legends, this is the fixed space between the axes edge and the legend. Default isrc['legend.borderaxespad']=0. If float, units are em-widths. If string, interpreted byunits.queue (
bool, optional) – IfTrueandlocis the same as an existing legend, the input arguments are added to a queue and this function returnsNone. This is used to “update” the same legend with successiveax.legend(...)calls. IfFalse(the default) andlocis the same as an existing inset legend, the old legend is removed. IfFalseandlocis an outer legend, the legends are stacked.
- Other Parameters
frame, frameon (
bool, optional) – Toggles the legend frame. For centered-row legends, a frame independent from matplotlib’s built-in legend frame is created.ncol, ncols (
int, optional) – The number of columns.ncolsis an alias, added for consistency withsubplots.order (
{'C', 'F'}, optional) – Whether legend handles are drawn in row-major ('C') or column-major ('F') order. Analagous tonumpy.arrayordering. Default is'F'.center (
bool, optional) – Whether to center each legend row individually. IfTrue, we draw successive single-row legends stacked on top of each other. IfNone, we infer this setting fromhandles. By default,centeris set toTrueifhandlesis a list of lists (each sublist is used as a row in the legend).alphabetize (
bool, optional) – Whether to alphabetize the legend entries according to the legend labels. Default isFalse.title, label (
str, optional) – The legend title. Thelabelkeyword is also accepted, for consistency withcolorbar.fontsize, fontweight, fontcolor (optional) – The font size, weight, and color for the legend text. Font size is interpreted by
units. The default font size isrc['legend.fontsize'].titlefontsize, titlefontweight, titlefontcolor (optional) – The font size, weight, and color for the legend title. Font size is interpreted by
units. The default size isfontsize.a, alpha, framealpha, fc, facecolor, framecolor, ec, edgecolor, ew, edgewidth (optional) – The opacity, face color, edge color, and edge width for the legend frame. Defaults are
rc['legend.framealpha']=0.8,rc['legend.facecolor']='white',rc['legend.edgecolor']='black'andrc['axes.linewidth']=0.6.color, lw, linewidth, m, marker, ls, linestyle, dashes, ms, markersize (
property-spec, optional) – Properties used to override the legend handles. For example, for a legend describing variations in line style ignoring variations in color, you might want to usecolor='k'.borderpad, borderaxespad, handlelength, handleheight, handletextpad, labelspacing, columnspacing (
floatorstr, optional) – Nativelegendspacing arguments interpreted withunits. The default units are still font size-relative.**kwargs – Passed to
legend.handler_map (dict-like, optional) – A dictionary mapping instances or types to a legend handler. This
handler_mapupdates the default handler map found atmatplotlib.legend.Legend.get_legend_handler_map.