The legend module defines the Legend class, which is responsible for drawing legends associated with axes and/or figures.
The Legend class can be considered as a container of legend handles and legend texts. Creation of corresponding legend handles from the plot elements in the axes or figures (e.g., lines, patches, etc.) are specified by the handler map, which defines the mapping between the plot elements and the legend handlers to be used (the default legend handlers are defined in the legend_handler module). Note that not all kinds of artist are supported by the legend yet (See Legend guide for more information).
Bases: matplotlib.offsetbox.DraggableOffsetBox
Bases: matplotlib.artist.Artist
Place a legend on the axes at location loc. Labels are a sequence of strings and loc can be a string or an integer specifying the legend location
The location codes are:
'best' : 0, (only implemented for axis legends)
'upper right' : 1,
'upper left' : 2,
'lower left' : 3,
'lower right' : 4,
'right' : 5,
'center left' : 6,
'center right' : 7,
'lower center' : 8,
'upper center' : 9,
'center' : 10,
loc can be a tuple of the noramilzed coordinate values with respect its parent.
parent: the artist that contains the legend
legend
labels: a list of strings to label the legend
Optional keyword arguments:
Keyword | Description |
---|---|
loc | a location code |
prop | the font property |
fontsize | the font size (used only if prop is not specified) |
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. |
The pad and spacing parameters are measured in font-size units. E.g., a fontsize of 10 points and a handlelength=5 implies a handlelength of 50 points. Values from rcParams will be used if None.
Users can specify any arbitrary location for the legend using the bbox_to_anchor keyword argument. bbox_to_anchor can be an instance of BboxBase(or its derivatives) or a tuple of 2 or 4 floats. See set_bbox_to_anchor() for more detail.
The legend location can be specified by setting loc with a tuple of 2 floats, which is interpreted as the lower-left corner of the legend in the normalized axes coordinate.
Set the draggable state – if state is
- None : toggle the current state
- True : turn draggable on
- False : turn draggable off
If draggable is on, you can drag the legend on the canvas with the mouse. The DraggableLegend helper instance is returned if draggable is on.
The update parameter control which parameter of the legend changes when dragged. If update is “loc”, the loc paramter of the legend is changed. If “bbox”, the bbox_to_anchor parameter is changed.
Draw everything that belongs to the legend
b is a boolean. Set draw frame to b
return the bbox that the legend will be anchored
return a list of child artists
A class method that returns the default handler map.
return the Rectangle instance used to frame the legend
Get whether the legend box patch is drawn
return a legend handler from legend_handler_map that corresponds to orig_handler.
legend_handler_map should be a dictionary object (that is returned by the get_legend_handler_map method).
It first checks if the orig_handle itself is a key in the legend_hanler_map and return the associated value. Otherwise, it checks for each of the classes in its method-resolution-order. If no matching key is found, it returns None.
return the handler map.
return a list of lines.Line2D instances in the legend
return a list of patch instances in the legend
return a list of text.Text instance in the legend
return Text instance for the legend title
return a extent of the the legend
set the bbox that the legend will be anchored.
bbox can be a BboxBase instance, a tuple of [left, bottom, width, height] in the given transform (normalized axes coordinate if None), or a tuple of [left, bottom] where the width and height will be assumed to be zero.
A class method to set the default handler map.
Set whether the legend box patch is drawn
ACCEPTS: [ True | False ]
set the legend title. Fontproperties can be optionally set with prop parameter.
A class method to update the default handler map.