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 by default but it is possible to extend the legend handler’s capabilities to support arbitrary objects. See the legend guide for more information.
update : If “loc”, update loc parameter of legend upon finalizing. If “bbox”, update bbox_to_anchor parameter.
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 normalized coordinate values with respect its parent.
parent: the artist that contains the legend
labels: a list of strings to label the legend
Optional keyword arguments:
|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|
|framealpha||If not None, alpha channel for the frame.|
|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 height 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.
This module defines default legend handlers.
It is strongly encouraged to have read the legend guide before this documentation.
Legend handlers are expected to be a callable object with a following signature.
legend_handler(legend, orig_handle, fontsize, handlebox)
Where legend is the legend itself, orig_handle is the original plot, fontsize is the fontsize in pixles, and handlebox is a OffsetBox instance. Within the call, you should create relevant artists (using relevant properties from the legend and/or orig_handle) and add them into the handlebox. The artists needs to be scaled according to the fontsize (note that the size is in pixel, i.e., this is dpi-scaled value).
This module includes definition of several legend handler classes derived from the base class (HandlerBase) with the following method.
def legend_artist(self, legend, orig_handle, fontsize, handlebox):
A Base class for default legend handlers.
The derived classes are meant to override create_artists method, which has a following signature.:
def create_artists(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans):
The overridden method needs to create artists of the given transform that fits in the given dimension (xdescent, ydescent, width, height) that are scaled by fontsize if necessary.
Return the artist that this HandlerBase generates for the given original artist/handle.
legend : matplotlib.legend.Legend instance
orig_handle : matplotlib.artist.Artist or similar
fontsize : float or int
handlebox : matplotlib.offsetbox.OffsetBox instance
Handler for CircleCollections
Handler for Errorbars
Handler for Line2D instances.
Handler for LineCollection instances.
Handler for Patch instances.
The HandlerPatch class optionally takes a function patch_func who’s responsibility is to create the legend key artist. The patch_func should have the signature:
def patch_func(legend=legend, orig_handle=orig_handle, xdescent=xdescent, ydescent=ydescent, width=width, height=height, fontsize=fontsize)
Subsequently the created artist will have its update_prop method called and the appropriate transform will be applied.
Handler for PathCollections, which are used by scatter
Handler for RegularPolyCollections.
Handler for Errorbars
Handler for Tuple