legend and legend_handler
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.
-
class matplotlib.legend.DraggableLegend(legend, use_blit=False, update='loc')
-
Bases:
matplotlib.offsetbox.DraggableOffsetBox
- update : If “loc”, update loc parameter of
- legend upon finalizing. If “bbox”, update bbox_to_anchor parameter.
-
artist_picker(legend, evt)
-
finalize_offset()
-
class 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, 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. 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 axes 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
-
- handles: a list of artists (lines, patches) to be added to the
-
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 markerfirst If true, place legend marker to left of label If false, place legend marker to right of label 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.
-
codes = {'lower center': 8, 'upper center': 9, 'lower right': 4, 'center right': 7, 'center left': 6, 'upper left': 2, 'center': 10, 'lower left': 3, 'right': 5, 'best': 0, 'upper right': 1}
-
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 paramter of the legend is changed. If “bbox”, the bbox_to_anchor parameter is changed.
-
draw(artist, renderer, *args, **kwargs)
-
Draw everything that belongs to the legend
-
draw_frame(b)
-
b is a boolean. Set draw frame to b
-
get_bbox_to_anchor()
-
return the bbox that the legend will be anchored
-
get_children()
-
return a list of child artists
-
classmethod get_default_handler_map()
-
A class method that returns the default handler map.
-
get_frame()
-
return the Rectangle instance used to frame the legend
-
get_frame_on()
-
Get whether the legend box patch is drawn
-
static 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_lines()
-
return a list of lines.Line2D instances in the legend
-
get_patches()
-
return a list of patch instances in the legend
-
get_texts()
-
return a list of text.Text instance in the legend
-
get_title()
-
return Text instance for the legend title
-
get_window_extent(*args, **kwargs)
-
return a extent of the legend
-
set_bbox_to_anchor(bbox, transform=None)
-
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.
-
classmethod 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
ACCEPTS: [ True | False ]
-
set_title(title, prop=None)
-
set the legend title. Fontproperties can be optionally set with prop parameter.
-
classmethod 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 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):-
class 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 :
matplotlib.legend.Legend
instanceThe legend for which these legend artists are being created.
orig_handle :
matplotlib.artist.Artist
or similarThe object for which these legend artists are being created.
fontsize : float or int
The fontsize in pixels. The artists being created should be scaled according to the given fontsize.
handlebox :
matplotlib.offsetbox.OffsetBox
instanceThe 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.
-
update_prop(legend_handle, orig_handle, legend)
-
-
class matplotlib.legend_handler.HandlerCircleCollection(yoffsets=None, sizes=None, **kw)
-
Handler for CircleCollections
-
create_collection(orig_handle, sizes, offsets, transOffset)
-
-
class 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)
-
-
class matplotlib.legend_handler.HandlerLine2D(marker_pad=0.3, numpoints=None, **kw)
-
Handler for Line2D instances.
-
create_artists(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)
-
-
class matplotlib.legend_handler.HandlerLineCollection(marker_pad=0.3, numpoints=None, **kw)
-
Handler for LineCollection instances.
-
create_artists(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)
-
get_numpoints(legend)
-
-
class matplotlib.legend_handler.HandlerNpoints(marker_pad=0.3, numpoints=None, **kw)
-
-
get_numpoints(legend)
-
get_xdata(legend, xdescent, ydescent, width, height, fontsize)
-
-
class matplotlib.legend_handler.HandlerNpointsYoffsets(numpoints=None, yoffsets=None, **kw)
-
-
get_ydata(legend, xdescent, ydescent, width, height, fontsize)
-
-
class matplotlib.legend_handler.HandlerPatch(patch_func=None, **kw)
-
Handler for Patch instances.
The HandlerPatch class optionally takes a function
patch_func
who’s responsibility is to create the legend key artist. Thepatch_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.-
create_artists(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)
-
-
class matplotlib.legend_handler.HandlerPathCollection(yoffsets=None, sizes=None, **kw)
-
Handler for PathCollections, which are used by scatter
-
create_collection(orig_handle, sizes, offsets, transOffset)
-
-
class 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)
-
-
class 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)
-
-
class matplotlib.legend_handler.HandlerStem(marker_pad=0.3, numpoints=None, bottom=None, yoffsets=None, **kw)
-
Handler for Errorbars
-
create_artists(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)
-
get_ydata(legend, xdescent, ydescent, width, height, fontsize)
-
-
class matplotlib.legend_handler.HandlerTuple(**kwargs)
-
Handler for Tuple
-
create_artists(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)
-
-
matplotlib.legend_handler.update_from_first_child(tgt, src)
© 2012–2016 Matplotlib Development Team. All rights reserved.
Licensed under the Matplotlib License Agreement.
http://matplotlib.org/1.5.3/api/legend_api.html