matplotlib.legend
¶The legend module defines the Legend class, which is responsible for drawing legends associated with axes and/or figures.
Important
It is unlikely that you would ever create a Legend instance manually.
Most users would normally create a legend via the
legend()
function. For more details on legends
there is also a legend guide.
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.
matplotlib.legend.
DraggableLegend
(legend, use_blit=False, update='loc')¶Bases: matplotlib.offsetbox.DraggableOffsetBox
Parameters: | update : string
|
---|
artist_picker
(legend, evt)¶finalize_offset
()¶matplotlib.legend.
Legend
(parent, handles, labels, loc=None, numpoints=None, markerscale=None, markerfirst=True, scatterpoints=None, scatteryoffsets=None, prop=None, fontsize=None, borderpad=None, labelspacing=None, handlelength=None, handleheight=None, handletextpad=None, borderaxespad=None, columnspacing=None, ncol=1, mode=None, fancybox=None, shadow=None, title=None, framealpha=None, edgecolor=None, facecolor=None, bbox_to_anchor=None, bbox_transform=None, frameon=None, handler_map=None)¶Bases: matplotlib.artist.Artist
Place a legend on the axes at location loc.
Parameters: |
handles : sequence of
labels : sequence of strings
|
||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Other Parameters: | |||||||||||||||||||||||||
loc : int or string or pair of floats, default: ‘upper right’
bbox_to_anchor :
ncol : integer
prop : None or
fontsize : int or float or {‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’}
numpoints : None or int
scatterpoints : None or int
scatteryoffsets : iterable of floats
markerscale : None or int or float
markerfirst : bool
frameon : None or bool
fancybox : None or bool
shadow : None or bool
framealpha : None or float
facecolor : None or “inherit” or a color spec edgecolor : None or “inherit” or a color spec mode : {“expand”, None}
bbox_transform : None or
title : str or None
borderpad : float or None
labelspacing : float or None
handlelength : float or None
handletextpad : float or None
borderaxespad : float or None
columnspacing : float or None
handler_map : dict or None
|
Notes
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.
codes
= {'best': 0, '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}¶contains
(event)¶draggable
(state=None, use_blit=False, update='loc')¶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 parameter of the legend is changed. If “bbox”, the bbox_to_anchor parameter is changed.
draw
(renderer)¶Draw everything that belongs to the legend.
draw_frame
(b)¶Set draw frame to b.
Parameters: | b : bool |
---|
get_bbox_to_anchor
()¶Return the bbox that the legend will be anchored to.
get_children
()¶Return a list of child artists.
get_default_handler_map
()¶A class method that returns the default handler map.
get_frame_on
()¶Get whether the legend box patch is drawn.
get_legend_handler
(legend_handler_map, orig_handle)¶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
.
get_legend_handler_map
()¶Return the handler map.
get_window_extent
(*args, **kwargs)¶Return extent of the legend.
set_bbox_to_anchor
(bbox, transform=None)¶Set the bbox that the legend will be anchored to.
bbox can be
BboxBase
instance(left, bottom, width, height)
in the given transform
(normalized axes coordinate if None)(left, bottom)
where the width and height will be
assumed to be zero.set_default_handler_map
(handler_map)¶A class method to set the default handler map.
set_frame_on
(b)¶Set whether the legend box patch is drawn.
Parameters: | b : bool |
---|
set_title
(title, prop=None)¶Set the legend title. Fontproperties can be optionally set with prop parameter.
update_default_handler_map
(handler_map)¶A class method to update the default handler map.
zorder
= 5¶matplotlib.legend_handler
¶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 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):
matplotlib.legend_handler.
HandlerBase
(xpad=0.0, ypad=0.0, update_func=None)¶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.
adjust_drawing_area
(legend, orig_handle, xdescent, ydescent, width, height, fontsize)¶create_artists
(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)¶legend_artist
(legend, orig_handle, fontsize, handlebox)¶Return the artist that this HandlerBase generates for the given original artist/handle.
Parameters: | legend :
orig_handle :
fontsize : float or int
handlebox :
|
---|
update_prop
(legend_handle, orig_handle, legend)¶matplotlib.legend_handler.
HandlerCircleCollection
(yoffsets=None, sizes=None, **kw)¶Handler for CircleCollections
.
create_collection
(orig_handle, sizes, offsets, transOffset)¶matplotlib.legend_handler.
HandlerErrorbar
(xerr_size=0.5, yerr_size=None, marker_pad=0.3, numpoints=None, **kw)¶Handler for Errorbars.
create_artists
(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)¶get_err_size
(legend, xdescent, ydescent, width, height, fontsize)¶matplotlib.legend_handler.
HandlerLine2D
(marker_pad=0.3, numpoints=None, **kw)¶Handler for Line2D
instances.
Parameters: | marker_pad : float
numpoints : int
|
---|
Notes
Any other keyword arguments are given to HandlerNpoints
.
create_artists
(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)¶matplotlib.legend_handler.
HandlerLineCollection
(marker_pad=0.3, numpoints=None, **kw)¶Handler for LineCollection
instances.
Parameters: | marker_pad : float
numpoints : int
|
---|
Notes
Any other keyword arguments are given to HandlerNpoints
.
create_artists
(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)¶get_numpoints
(legend)¶matplotlib.legend_handler.
HandlerNpoints
(marker_pad=0.3, numpoints=None, **kw)¶A legend handler that shows numpoints points in the legend entry.
Parameters: | marker_pad : float
numpoints : int
|
---|
Notes
Any other keyword arguments are given to HandlerBase
.
get_numpoints
(legend)¶get_xdata
(legend, xdescent, ydescent, width, height, fontsize)¶matplotlib.legend_handler.
HandlerNpointsYoffsets
(numpoints=None, yoffsets=None, **kw)¶A legend handler that shows numpoints in the legend, and allows them to be individually offest in the y-direction.
Parameters: | numpoints : int
yoffsets : array of floats
|
---|
Notes
Any other keyword arguments are given to HandlerNpoints
.
get_ydata
(legend, xdescent, ydescent, width, height, fontsize)¶matplotlib.legend_handler.
HandlerPatch
(patch_func=None, **kw)¶Handler for Patch
instances.
Parameters: | patch_func : callable, optional
|
---|
Notes
Any other keyword arguments are given to HandlerBase
.
create_artists
(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)¶matplotlib.legend_handler.
HandlerPathCollection
(yoffsets=None, sizes=None, **kw)¶Handler for PathCollections
, which are used by scatter
.
create_collection
(orig_handle, sizes, offsets, transOffset)¶matplotlib.legend_handler.
HandlerPolyCollection
(xpad=0.0, ypad=0.0, update_func=None)¶Handler for PolyCollection
used in fill_between
and stackplot
.
create_artists
(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)¶matplotlib.legend_handler.
HandlerRegularPolyCollection
(yoffsets=None, sizes=None, **kw)¶Handler for RegularPolyCollections
.
create_artists
(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)¶create_collection
(orig_handle, sizes, offsets, transOffset)¶get_numpoints
(legend)¶get_sizes
(legend, orig_handle, xdescent, ydescent, width, height, fontsize)¶update_prop
(legend_handle, orig_handle, legend)¶matplotlib.legend_handler.
HandlerStem
(marker_pad=0.3, numpoints=None, bottom=None, yoffsets=None, **kw)¶Handler for plots produced by stem
.
Parameters: | marker_pad : float
numpoints : int, optional
bottom : float, optional yoffsets : array of floats, optional
|
---|
Notes
Any other keyword arguments are given to HandlerNpointsYoffsets
.
create_artists
(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)¶get_ydata
(legend, xdescent, ydescent, width, height, fontsize)¶matplotlib.legend_handler.
HandlerTuple
(ndivide=1, pad=None, **kwargs)¶Handler for Tuple.
Additional kwargs are passed through to HandlerBase
.
Parameters: | ndivide : int, optional
pad : float, optional
|
---|
create_artists
(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)¶matplotlib.legend_handler.
update_from_first_child
(tgt, src)¶