@@ -283,38 +283,83 @@ def legend(self, *args, **kwargs):
283
283
"""
284
284
Places a legend on the axes.
285
285
286
- To make a legend for lines which already exist on the axes
287
- (via plot for instance), simply call this function with an iterable
288
- of strings, one for each legend item. For example::
286
+ Call signatures::
289
287
290
- ax.plot([1, 2, 3])
291
- ax.legend(['A simple line'])
288
+ legend()
289
+ legend(labels)
290
+ legend(handles, labels)
291
+
292
+ The call signatures correspond to three different ways how to use
293
+ this method.
292
294
293
- However, in order to keep the "label" and the legend element
294
- instance together, it is preferable to specify the label either at
295
- artist creation, or by calling the
296
- :meth:`~matplotlib.artist.Artist.set_label` method on the artist::
295
+ **1. Automatic detection of elements to be shown in the legend**
296
+
297
+ The elements to be added to the legend are automatically determined,
298
+ when you do not pass in any extra arguments.
299
+
300
+ In this case, the labels are taken from the artist. You can specify
301
+ them either at artist creation or by calling the
302
+ :meth:`~.Artist.set_label` method on the artist::
297
303
298
304
line, = ax.plot([1, 2, 3], label='Inline label')
299
- # Overwrite the label by calling the method.
305
+ ax.legend()
306
+
307
+ or::
308
+
300
309
line.set_label('Label via method')
310
+ line, = ax.plot([1, 2, 3])
301
311
ax.legend()
302
312
303
313
Specific lines can be excluded from the automatic legend element
304
314
selection by defining a label starting with an underscore.
305
- This is default for all artists, so calling :meth:` legend` without
315
+ This is default for all artists, so calling `Axes. legend` without
306
316
any arguments and without setting the labels manually will result in
307
317
no legend being drawn.
308
318
319
+
320
+ **2. Labeling existing plot elements**
321
+
322
+ To make a legend for lines which already exist on the axes
323
+ (via plot for instance), simply call this function with an iterable
324
+ of strings, one for each legend item. For example::
325
+
326
+ ax.plot([1, 2, 3])
327
+ ax.legend(['A simple line'])
328
+
329
+ Note: This way of using is discouraged, because the relation between
330
+ plot elements and labels is only implicit by their order and can
331
+ easily be mixed up.
332
+
333
+
334
+ **3. Explicitly defining the elements in the legend**
335
+
309
336
For full control of which artists have a legend entry, it is possible
310
337
to pass an iterable of legend artists followed by an iterable of
311
338
legend labels respectively::
312
339
313
- legend((line1, line2, line3), ('label1', 'label2', 'label3'))
340
+ legend((line1, line2, line3), ('label1', 'label2', 'label3'))
314
341
315
342
Parameters
316
343
----------
317
344
345
+ handles : sequence of `~.Artist`, optional
346
+ A list of Artists (lines, patches) to be added to the legend.
347
+ Use this together with *labels*, if you need full control on what
348
+ is shown in the legend and the automatic mechanism described above
349
+ is not sufficient.
350
+
351
+ The length of handles and labels should be the same in this
352
+ case. If they are not, they are truncated to the smaller length.
353
+
354
+ labels : sequence of strings, optional
355
+ A list of labels to show next to the artists.
356
+ Use this together with *handles*, if you need full control on what
357
+ is shown in the legend and the automatic mechanism described above
358
+ is not sufficient.
359
+
360
+ Other Parameters
361
+ ----------------
362
+
318
363
loc : int or string or pair of floats, default: 'upper right'
319
364
The location of the legend. Possible codes are:
320
365
0 commit comments