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')[source]¶Bases: matplotlib.offsetbox.DraggableOffsetBox
Wrapper around a Legend
to support mouse dragging.
Parameters: | legend :
use_blit : bool, optional
update : {'loc', 'bbox'}, optional
|
---|
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, title_fontsize=None, framealpha=None, edgecolor=None, facecolor=None, bbox_to_anchor=None, bbox_transform=None, frameon=None, handler_map=None)[source]¶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:
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
title_fontsize: 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, 'center': 10, 'center left': 6, 'center right': 7, 'lower center': 8, 'lower left': 3, 'lower right': 4, 'right': 5, 'upper center': 9, 'upper left': 2, 'upper right': 1}¶contains
(event)[source]¶Test whether the artist contains the mouse event.
Returns the truth value and a dictionary of artist specific details of selection, such as which points are contained in the pick radius. See individual artists for details.
draggable
(state=None, use_blit=False, update='loc')[source]¶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.
get_legend_handler
(legend_handler_map, orig_handle)[source]¶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_tightbbox
(renderer)[source]¶Like Legend.get_window_extent
, but uses the box for the legend.
Parameters: | renderer :
|
---|---|
Returns: | `.BboxBase` : containing the bounding box in figure pixel co-ordinates. |
set_bbox_to_anchor
(bbox, transform=None)[source]¶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)[source]¶A class method to set the default handler map.
set_draggable
(state, use_blit=False, update='loc')[source]¶Enable or disable mouse dragging support of the legend.
Parameters: | state : bool
use_blit : bool, optional
update : {'loc', 'bbox'}, optional
|
---|---|
Returns: | If state is instance. Otherwise this returns |
set_title
(title, prop=None)[source]¶Set the legend title. Fontproperties can be optionally set with prop parameter.
update_default_handler_map
(handler_map)[source]¶A class method to update the default handler map.
zorder
= 5¶