Axes.
legend
(*args, **kwargs)¶Places a legend on the axes.
Call signatures:
legend()
legend(labels)
legend(handles, labels)
The call signatures correspond to three different ways how to use this method.
1. Automatic detection of elements to be shown in the legend
The elements to be added to the legend are automatically determined, when you do not pass in any extra arguments.
In this case, the labels are taken from the artist. You can specify
them either at artist creation or by calling the
set_label()
method on the artist:
line, = ax.plot([1, 2, 3], label='Inline label')
ax.legend()
or:
line.set_label('Label via method')
line, = ax.plot([1, 2, 3])
ax.legend()
Specific lines can be excluded from the automatic legend element
selection by defining a label starting with an underscore.
This is default for all artists, so calling Axes.legend
without
any arguments and without setting the labels manually will result in
no legend being drawn.
2. Labeling existing plot elements
To make a legend for lines which already exist on the axes (via plot for instance), simply call this function with an iterable of strings, one for each legend item. For example:
ax.plot([1, 2, 3])
ax.legend(['A simple line'])
Note: This way of using is discouraged, because the relation between plot elements and labels is only implicit by their order and can easily be mixed up.
3. Explicitly defining the elements in the legend
For full control of which artists have a legend entry, it is possible to pass an iterable of legend artists followed by an iterable of legend labels respectively:
legend((line1, line2, line3), ('label1', 'label2', 'label3'))
Parameters: | handles : sequence of
labels : sequence of strings, optional
|
||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns: |
|
||||||||||||||||||||||||
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
Not all kinds of artist are supported by the legend command. See Legend guide for details.
Examples
(Source code, png, pdf)