You are reading an old version of the documentation (v1.3.0). For the latest version see

Table Of Contents

Previous topic


Next topic


This Page



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 (See 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)
class matplotlib.legend.Legend(parent, handles, labels, loc=None, numpoints=None, markerscale=None, 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 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

  • handles: a list of artists (lines, patches) to be added to the


  • 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
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 length 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 = {'right': 5, 'center left': 6, 'upper right': 1, 'lower right': 4, 'best': 0, 'center': 10, 'lower left': 3, 'center right': 7, 'upper left': 2, 'upper center': 9, 'lower center': 8}
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


b is a boolean. Set draw frame to b


return the bbox that the legend will be anchored


return a list of child artists

classmethod get_default_handler_map()

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

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.


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

get_window_extent(*args, **kwargs)

return a extent of the 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 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