MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / legend

Method legend

lib/matplotlib/axes/_axes.py:234–354  ·  view source on GitHub ↗

Place a legend on the Axes. Call signatures:: legend() legend(handles, labels) legend(handles=handles) legend(labels) The call signatures correspond to the following different ways to use this method: **1. A

(self, *args, **kwargs)

Source from the content-addressed store, hash-verified

232
233 @_docstring.interpd
234 def legend(self, *args, **kwargs):
235 """
236 Place a legend on the Axes.
237
238 Call signatures::
239
240 legend()
241 legend(handles, labels)
242 legend(handles=handles)
243 legend(labels)
244
245 The call signatures correspond to the following different ways to use
246 this method:
247
248 **1. Automatic detection of elements to be shown in the legend**
249
250 The elements to be added to the legend are automatically determined,
251 when you do not pass in any extra arguments.
252
253 In this case, the labels are taken from the artist. You can specify
254 them either at artist creation or by calling the
255 :meth:`~.Artist.set_label` method on the artist::
256
257 ax.plot([1, 2, 3], label='Inline label')
258 ax.legend()
259
260 or::
261
262 line, = ax.plot([1, 2, 3])
263 line.set_label('Label via method')
264 ax.legend()
265
266 .. note::
267 Specific artists can be excluded from the automatic legend element
268 selection by using a label starting with an underscore, "_".
269 A string starting with an underscore is the default label for all
270 artists, so calling `.Axes.legend` without any arguments and
271 without setting the labels manually will result in a ``UserWarning``
272 and an empty legend being drawn.
273
274
275 **2. Explicitly listing the artists and labels in the legend**
276
277 For full control of which artists have a legend entry, it is possible
278 to pass an iterable of legend artists followed by an iterable of
279 legend labels respectively::
280
281 ax.legend([line1, line2, line3], ['label1', 'label2', 'label3'])
282
283
284 **3. Explicitly listing the artists in the legend**
285
286 This is similar to 2, but the labels are taken from the artists'
287 label properties. Example::
288
289 line1, = ax.plot([1, 2, 3], label='label1')
290 line2, = ax.plot([1, 2, 3], label='label2')
291 ax.legend(handles=[line1, line2])

Callers 15

run_memleak_testFunction · 0.45
test_errorbar3dFunction · 0.45
test_legend_plotFunction · 0.45
test_legend_barFunction · 0.45
test_fancyFunction · 0.45
test_handlerline3dFunction · 0.45
figlegendFunction · 0.45
legendFunction · 0.45
apply_callbackFunction · 0.45
test_gidFunction · 0.45

Calls

no outgoing calls

Tested by 15

test_errorbar3dFunction · 0.36
test_legend_plotFunction · 0.36
test_legend_barFunction · 0.36
test_fancyFunction · 0.36
test_handlerline3dFunction · 0.36
test_gidFunction · 0.36
test_legend_colorsFunction · 0.36
test_simpleFunction · 0.36