Default legend handlers.
Important
This is a low-level legend API, which most end users do not need.
We recommend that you are familiar with the legend guide before reading 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 pixels, 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
The legend for which these legend artists are being created.
matplotlib.artist.Artist
or similar
The object for which these legend artists are being created.
The fontsize in pixels. The artists being created should be scaled according to the given fontsize.
matplotlib.offsetbox.OffsetBox
The box which has been created to hold this legend entry's artists. Artists created in the legend_artist
method must be added to this handlebox inside this method.
Handler for CircleCollection
s.
Number of points to show in legend entry.
Length numpoints list of y offsets for each point in legend entry.
Keyword arguments forwarded to HandlerNpoints
.
Handler for Errorbars.
Padding between points in legend entry.
Number of points to show in legend entry.
Keyword arguments forwarded to HandlerNpoints
.
Handler for Line2D
instances.
See also
HandlerLine2DCompound
An earlier handler implementation, which used one artist for the line and another for the marker(s).
Padding between points in legend entry.
Number of points to show in legend entry.
Keyword arguments forwarded to HandlerNpoints
.
Original handler for Line2D
instances, that relies on combining a line-only with a marker-only artist. May be deprecated in the future.
Padding between points in legend entry.
Number of points to show in legend entry.
Keyword arguments forwarded to HandlerNpoints
.
Handler for LineCollection
instances.
Padding between points in legend entry.
Number of points to show in legend entry.
Keyword arguments forwarded to HandlerNpoints
.
A legend handler that shows numpoints points in the legend entry.
Padding between points in legend entry.
Number of points to show in legend entry.
Keyword arguments forwarded to HandlerBase
.
A legend handler that shows numpoints in the legend, and allows them to be individually offset in the y-direction.
Number of points to show in legend entry.
Length numpoints list of y offsets for each point in legend entry.
Keyword arguments forwarded to HandlerNpoints
.
Handler for Patch
instances.
The function that creates the legend key artist. 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.
Keyword arguments forwarded to HandlerBase
.
Handler for PathCollection
s, which are used by scatter
.
Number of points to show in legend entry.
Length numpoints list of y offsets for each point in legend entry.
Keyword arguments forwarded to HandlerNpoints
.
Handler for PolyCollection
used in fill_between
and stackplot
.
Handler for RegularPolyCollection
s.
Number of points to show in legend entry.
Length numpoints list of y offsets for each point in legend entry.
Keyword arguments forwarded to HandlerNpoints
.
Handler for plots produced by stem
.
Padding between points in legend entry.
Number of points to show in legend entry.
Length numpoints list of y offsets for each point in legend entry.
Keyword arguments forwarded to HandlerNpointsYoffsets
.
Handler for StepPatch
instances.
Handler for Tuple.
The number of sections to divide the legend area into. If None, use the length of the input tuple.
rcParams["legend.borderpad"]
(default: 0.4
)
Padding in units of fraction of font size.
Keyword arguments forwarded to HandlerBase
.
© 2012–2021 Matplotlib Development Team. All rights reserved.
Licensed under the Matplotlib License Agreement.
https://matplotlib.org/3.5.1/api/legend_handler_api.html